Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to get C++ code that uses the curl.h library to make curl requests that require the setting of the Authorization: Bearer header. I am using Linux Mint 18 (Ubuntu).

I have made this curl request from the command line, and it works, it looks likes this:

curl -H "Content-Type: application/json" -H "Authorization: Bearer <my_token>" <my_url>

Doing this returns a valid result.

However, I tried to write the code equivalent for this in C++ using the curl.h library, and I got {"errorMessage":"Insufficient authorization to perform request."}

Here is the C++ code I used:

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <curl/curl.h>

using namespace std;

int main(int argc, char** argv)
{
    CURL* curl = curl_easy_init();

    if (!curl) {
        cerr << "curl initialization failure" << endl;
        return 1;
    }

    CURLcode res;

    // the actual code has the actual url string in place of <my_url>
    curl_easy_setopt(curl, CURLOPT_URL, <my_url>);

    struct curl_slist* headers = NULL;
    curl_slist_append(headers, "Content-Type: application/json");

    // the actual code has the actual token in place of <my_token>
    curl_slist_append(headers, "Authorization: Bearer <my_token>");

    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

    res = curl_easy_perform(curl);

    if (res != CURLE_OK) {
        cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << endl;
    }

    curl_easy_cleanup(curl);

    return 0;
}

I have also tried using a line that looks like this:

curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, <my_token>);

in place of this line:

curl_slist_append(headers, "Authorization: Bearer <my_token>");

I have also tried adding these lines too before curl_easy_perform:

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

and

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

because I saw those lines when searching around

But all my efforts of figuring out how to get this working is still leaving me with "errorMessage: insufficient authorization to perform request."

And, yes, I have by the way made sure that both the url and the token I am using are correct.

What am I doing wrong, and how do I properly translate the command line code at the top to the equivalent C++ code.

like image 562
user904542 Avatar asked Jul 26 '18 14:07

user904542


1 Answers

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);

like image 80
Ingo Leonhardt Avatar answered Sep 20 '22 17:09

Ingo Leonhardt