-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurls_utils.cpp
175 lines (137 loc) · 5.58 KB
/
curls_utils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "curls_utils.h"
#include <curl/curl.h>
#include<nlohmann/json.hpp>
#include<iostream>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string curls_utils::base64_encode(const std::string &in) {
std::string out;
int val = 0, valb = -6;
for (unsigned char c : in) {
val = (val << 8) + c;
valb += 8;
while (valb >= 0) {
out.push_back(base64_chars[(val >> valb) & 0x3F]);
valb -= 6;
}
}
if (valb > -6) out.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]);
while (out.size() % 4) out.push_back('=');
return out;
}
size_t curls_utils::WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
size_t totalSize = size * nmemb;
std::string* str = static_cast<std::string*>(userp);
str->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
json curls_utils::GetCurrentUserInfo(std::string accessToken){
CURL* curl;
CURLcode res;
std::string readBuffer;
std::string url = "https://api.spotify.com/v1/me";
// Initialize libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if(curl) {
// Set the URL
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
std::string headerString = "Authorization: Bearer "+accessToken;
const char* headerCString= headerString.c_str();
// Set the Content-Type header
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, headerCString);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set the write callback function to capture the response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curls_utils::WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// Perform the POST request
res = curl_easy_perform(curl);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
} else {
}
// Clean up
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
// Global cleanup
curl_global_cleanup();
return json::parse(readBuffer);
}
json curls_utils::GetAccessToken(std::string authCode, std::string clientId, std::string clientSecret) {
CURL* curl;
CURLcode res;
std::string readBuffer;
std::string url = "https://accounts.spotify.com/api/token";
std::string postData = "code=" + authCode +
"&redirect_uri=http://localhost:8888/callback" +
"&grant_type=authorization_code";
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
// Set URL and post data
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
// SSL verification (keep enabled if possible)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
// Set headers
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, ("Authorization: Basic " + curls_utils::base64_encode(clientId+":"+clientSecret)).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set write callback
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curls_utils::WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// Perform the request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
curl_global_cleanup();
return json::parse(readBuffer);
}
json curls_utils::GetAccessTokenRefresh(std::string refreshToken, std::string clientId, std::string clientSecret){
CURL* curl;
CURLcode res;
std::string readBuffer;
std::string url = "https://accounts.spotify.com/api/token";
std::string postData = "grant_type=refresh_token"
"&refresh_token="+refreshToken;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
// Set URL and post data
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData.c_str());
// SSL verification (keep enabled if possible)
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
// Set headers
struct curl_slist* headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/x-www-form-urlencoded");
headers = curl_slist_append(headers, ("Authorization: Basic " + curls_utils::base64_encode(clientId+":"+clientSecret)).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
// Set write callback
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curls_utils::WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// Perform the request
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
}
// Cleanup
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
}
curl_global_cleanup();
return json::parse(readBuffer);
}