SajithP
SajithP

Reputation: 587

Using LIBCURL in C to implement a GET call with Bearer Token authorization

I'm writing a simple program to use LIBCURL in C to connect to a HTTPS REST service. To verify the endpoint I used POSTMAN and the GET request returned a successful response. REST service uses Bearer token authorization.

But when I implemented it in C, it gives me the following error. I used the auto generated code given by POSTMAN.

* Connected to abcxyz.com (13.224.177.6) port 443 (#0)
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol
* Closing connection 0 curl_easy_perform() failed: SSL connect error

I tried using below with different combinations according to what I found on internet, but didn't help me.

curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_NONE);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); //only for https
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); //only for https
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_ALLOW_BEAST | CURLSSLOPT_NO_REVOKE);
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);

This is my code snippet,

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
  curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
  curl_easy_setopt(curl, CURLOPT_URL, "abcxyz.com/v1/service-invenue/configs?keys=SSC_AVAILABLE&keys=SSC_AVAILABLE");
  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
  curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
  struct curl_slist *headers = NULL;
  headers = curl_slist_append(headers, "Authorization: Bearer 5065157d2873736ddd726df6678ddh972413");
  curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  res = curl_easy_perform(curl);

  if(res != CURLE_OK)
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
  curl_easy_cleanup(curl);

Really appreciate if anyone could help me out to sort out my C program. Thanks!

I even tried the equivalent python code, it gives me below error (If this helps anyone to understand the issue)

File "/python_root/lib/ssl.py", line 601, in __init__
    self.do_handshake()
File "/python_root/lib/ssl.py", line 830, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

Python code given by POSTMAN

import http.client
import mimetypes
conn = http.client.HTTPSConnection("abcxyz.com")
payload = ''
headers = {
  'Authorization': 'Bearer 5065157d2873736ddd726df6678ddh972413'
}
conn.request("GET", "/v1/service-invenue/configs?keys=SSC_AVAILABLE&keys=SSC_AVAILABLE", payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))

Upvotes: 0

Views: 981

Answers (1)

Cibin Chungath
Cibin Chungath

Reputation: 11

I received output for this code .....Below is my Code Snippet.

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) 
{
    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "GET");
    curl_easy_setopt(curl, CURLOPT_URL, "https://.....");
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
    curl_easy_setopt(curl, CURLOPT_DEFAULT_PROTOCOL, "https");
    struct curl_slist *headers = NULL;
    headers = curl_slist_append(headers, "Authorization: Bearer .......");
    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
    res = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);

Upvotes: 1

Related Questions