From db3ab59daf0f7833d0411e5b84670af5144711b0 Mon Sep 17 00:00:00 2001 From: Jack Date: Wed, 20 Jul 2016 22:57:47 +0100 Subject: [PATCH] Better compliance with API reference Send User-Agent header, move token auth to Authorization header. https://discordapp.com/developers/docs/reference --- TriviaBot/bot/APIHelper.cpp | 6 ++---- TriviaBot/bot/APIHelper.hpp | 2 -- TriviaBot/bot/http/HTTPHelper.cpp | 13 +++++++++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/TriviaBot/bot/APIHelper.cpp b/TriviaBot/bot/APIHelper.cpp index 023eabd..13bc476 100644 --- a/TriviaBot/bot/APIHelper.cpp +++ b/TriviaBot/bot/APIHelper.cpp @@ -4,15 +4,13 @@ #include "APIHelper.hpp" -extern std::string bot_token; - APIHelper::APIHelper() : BASE_URL("https://discordapp.com/api"), CHANNELS_URL(BASE_URL + "/channels"), - TOKEN_PARAM("token=" + bot_token), JSON_CTYPE("application/json") { + JSON_CTYPE("application/json") { http = new HTTPHelper(); } void APIHelper::send_message(std::string channel_id, std::string message) { - const std::string url = CHANNELS_URL + "/" + channel_id + "/messages?" + TOKEN_PARAM; + const std::string url = CHANNELS_URL + "/" + channel_id + "/messages"; json data = { { "content", message } }; diff --git a/TriviaBot/bot/APIHelper.hpp b/TriviaBot/bot/APIHelper.hpp index 3e00658..5859463 100644 --- a/TriviaBot/bot/APIHelper.hpp +++ b/TriviaBot/bot/APIHelper.hpp @@ -18,8 +18,6 @@ public: private: const std::string BASE_URL; const std::string CHANNELS_URL; - const std::string TOKEN; - const std::string TOKEN_PARAM; const std::string JSON_CTYPE; HTTPHelper *http; diff --git a/TriviaBot/bot/http/HTTPHelper.cpp b/TriviaBot/bot/http/HTTPHelper.cpp index b002069..18b2ab6 100644 --- a/TriviaBot/bot/http/HTTPHelper.cpp +++ b/TriviaBot/bot/http/HTTPHelper.cpp @@ -1,5 +1,7 @@ #include "HTTPHelper.hpp" +extern std::string bot_token; + /* * Warning: (Awful) C Code */ @@ -17,8 +19,15 @@ std::string HTTPHelper::post_request(std::string url, std::string content_type, // Now with real HTTPS! curl_easy_setopt(curl, CURLOPT_CAINFO, "bot/http/DiscordCA.crt"); - std::string content_header = "Content-Type: " + content_type; - headers = curl_slist_append(headers, content_header.c_str()); + std::string header_arr[3]; + header_arr[0] = "Content-Type: " + content_type; + header_arr[1] = "Authorization: Bot " + bot_token; + header_arr[2] = "User-Agent: DiscordBot(http://github.com/jackb-p/triviadiscord, 1.0)"; + + for (std::string h : header_arr) { + headers = curl_slist_append(headers, h.c_str()); + } + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());