How do I set authorization bearer header in C++ curl code? I'm getting insufficient authorization, eventhough it works at the command line

You need to assign the return value of curl_slist_append() to headers in every call like that:

headers = curl_slist_append(headers, "Content-Type: application/json");
headers = curl_slist_append(headers, "Authorization: Bearer <my_token>");

See this doc

The way you call it headers will always remain NULL and that's what you pass to curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);


I had the same problem with CURLOPT_XOAUTH2_BEARER. The solution was to set CURLOPT_HTTPAUTH to CURLAUTH_BEARER, like this:

curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, "<my_token>");
curl_easy_setopt(_connection, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);

CURLAUTH_BEARER was added in 7.61.0. If your libcurl is older, CURLAUTH_ANY should work.