Creating https with libcurl


I am trying to connect to google api.
This works fine in my terminal, there I do:
curl https://www.googleapis.com/tasks/v1/users/@me/lists --header "Authorization: Bearer myAccessCode" .
This works great, but now I want to do it inside the c program.
For this, I:

  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 just get a message that a login is required. I have no idea what I'm doing wrong, is there someone who sees my failure?

+6
source share
1 answer

As I wrote in the comment above:
I just realized what I did: curl_slist_append(headers, header);
instead: headers = curl_slist_append(headers, header);
Therefore, the headers were always NULL, and I made a request to receive without a header.
(I edited it in my question above, so the code works if there is one)

+4
source

Source: https://habr.com/ru/post/913533/


All Articles