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

2

10

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.

Mandle answered 26/7, 2018 at 14:2 Comment(1)
There's no language named "C/C++". The code you're showing here is C++.Singular
R
14

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

Redoubtable answered 26/7, 2018 at 14:9 Comment(2)
Oh, thank you very much, that fixed it! And thank you for being so fast! I tried to approve your answer, but it was so fast, that I have to wait another 5 minutes!Mandle
Thanks.. wasted my whole morning trying to figure this out.Metabolite
S
3

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.

Shlomo answered 10/1, 2020 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.