Making https get with libcurl

2020-06-17 04:46发布


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?

标签: c curl libcurl
1条回答
beautiful°
2楼-- · 2020-06-17 04:59

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)

查看更多
登录 后发表回答