使用libcurl使https获取 [英] Making https get with libcurl
问题描述
我正在尝试连接到Google API.
这在我的终端中正常运行,我正在这样做:
curl https://www.googleapis.com/tasks/v1/users/@me/lists --header "Authorization: Bearer myAccessCode"
.
效果很好,但现在我想在c程序中制作它.
为此,我有:
I'm trying to connect to a google api.
This works fine in my terminal, there I'm doing:
curl https://www.googleapis.com/tasks/v1/users/@me/lists --header "Authorization: Bearer myAccessCode"
.
This works fine, but now I want to make this inside a c program.
For this I have:
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);
但是在这里,我只是收到一条消息,要求登录. 我不知道我在做什么错,有人看到我的失败吗?
But here I'm just getting a message that a login is required. I have no idea what I'm doing wrong, is there someone who sees my failure?
推荐答案
就像我在上面的评论中所写的那样:
我刚刚意识到自己做了:curl_slist_append(headers, header);
代替:headers = curl_slist_append(headers, header);
所以标头始终为NULL,我没有标头就发出了get请求.
(我在上面的问题中对其进行了编辑,因此该代码可以运行,如果有的话)
Like I wrote in the comment above:
I just realized that I made: curl_slist_append(headers, header);
instead of: headers = curl_slist_append(headers, header);
So headers was always NULL and I made the get request without a header.
(I edited it in my question above, so the code works, if some)
这篇关于使用libcurl使https获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!