diff options
| author | Aotrix <[email protected]> | 2025-03-07 19:19:18 +0100 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2025-03-07 19:19:18 +0100 |
| commit | 672a39e969b0764ddd0839573c92fc662669f79f (patch) | |
| tree | 8e2074c7a7f5b161388274b08680b6f22b527dd7 /src/main.cpp | |
| parent | db937e7b0feac074e8e3957d2b885a02bbe82a13 (diff) | |
added token request
Diffstat (limited to 'src/main.cpp')
| -rw-r--r-- | src/main.cpp | 53 |
1 files changed, 42 insertions, 11 deletions
diff --git a/src/main.cpp b/src/main.cpp index 9ff2856..dbc9f8e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,7 @@ +#include <algorithm> +#include <cctype> #include <curl/curl.h> +#include <curl/easy.h> #include <format> #include <iostream> #include <pqxx/pqxx> @@ -13,6 +16,8 @@ void do_curl_request(const char *url, const char *header) { CURL *curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); struct curl_slist *chunk = nullptr; chunk = curl_slist_append(chunk, header); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); @@ -39,10 +44,38 @@ bool is_last_generated_token_still_valid() { return false; } -std::string get_new_token() { - // TODO: - // curl -u 2b50f1123b424d5e9c21971a110fdd04:MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6 - // -d grant_type=client_credentials https://oauth.battle.net/token +size_t curl_callback_token_request(void *contents, size_t size, size_t nmemb, + std::string *s) { + size_t newLength = size * nmemb; + try { + s->append((char *)contents, newLength); + } catch (std::bad_alloc &e) { + // handle memory problem + return 0; + } + return newLength; +} + +std::string get_valid_token() { + // TODO: retrieve token from output + CURL *curl = curl_easy_init(); + std::string output; + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, "https://oauth.battle.net/token"); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); + curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, + curl_callback_token_request); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &output); + curl_easy_setopt(curl, CURLOPT_USERPWD, + "2b50f1123b424d5e9c21971a110fdd04:" + "MrMkhVMNdOeqPL4Q4XVQAsRPR14J7Yq6"); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, + "grant_type=client_credentials"); + curl_easy_perform(curl); + curl_easy_cleanup(curl); + } + std::cout << output << std::endl; return ""; } @@ -60,12 +93,10 @@ int main() { pqxx::connection cx{ "postgresql://aotrix@localhost:5432/hearthstonegameinfo"}; pqxx::work tx{cx}; - std::string access_token{"EU5xHPtrrZdPWXDBDe26WJuNnG0wyXk1rl"}; - // don't need to call the function every time - std::string header = generate_request_header(access_token); - // get_url(), otherwise... - do_curl_request(get_url(), header); - std::cout << std::endl << std::endl; - display_result(execute_query(tx, "SELECT * FROM cards")); + std::string access_token = get_valid_token(); + // std::string header = generate_request_header(access_token); + // do_curl_request(get_url(), header); + // std::cout << std::endl << std::endl; + // display_result(execute_query(tx, "SELECT * FROM cards")); return 0; } |
