Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making https get with libcurl

Tags:

c

curl

libcurl


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?

like image 964
Stephan Avatar asked Apr 18 '12 13:04

Stephan


1 Answers

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)

like image 53
Stephan Avatar answered Sep 28 '22 04:09

Stephan