我正在尝试连接到谷歌API.
这在我的终端工作正常,我正在做:
curl https://www.googleapis.com/tasks/v1/users/@me/lists --header "Authorization: Bearer myAccessCode".
这工作正常,但现在我想在内部交流程序.
为此,我有:
CURL *curl;
char *header = "Authorization: Bearer myAccessCode";
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, header);
curl = curl_easy_init();
char *response = NULL;
curl_easy_setopt(curl, CURLOPT_URL, "https://www.googleapis.com/tasks/v1/users/@me/lists");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, httpsCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
Run Code Online (Sandbox Code Playgroud)
但在这里,我只是收到一条需要登录的消息.我不知道我做错了什么,是否有人看到我的失败?
就像我在上面的注释中写的:
我只是意识到我做了:curl_slist_append(headers, header);
而不是:headers = curl_slist_append(headers, header);
所以标头始终为NULL,并且我在没有标头的情况下发出了get请求。
(我在上面的问题中对其进行了编辑,因此该代码可以运行,如果有的话)