简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM