【问题标题】:Using LIBCURL in C to implement a GET call with Bearer Token authorization在 C 中使用 LIBCURL 实现带有 Bearer Token 授权的 GET 调用
【发布时间】:2020-04-29 14:06:29
【问题描述】:

我正在编写一个简单的程序来使用 C 语言中的 LIBCURL 连接到 HTTPS REST 服务。为了验证端点,我使用了 POSTMAN 并且 GET 请求返回了成功的响应。 REST 服务使用 Bearer 令牌授权。

但是当我在 C 中实现它时,它给了我以下错误。我使用了 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

根据我在互联网上找到的内容,我尝试使用以下不同的组合,但没有帮助。

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

这是我的代码 sn-p,

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

如果有人可以帮助我整理我的 C 程序,我将不胜感激。谢谢!

我什至尝试了等效的 python 代码,它给了我以下错误(如果这有助于任何人理解问题)

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)

POSTMAN 提供的 Python 代码

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

【问题讨论】:

    标签: python c curl libcurl


    【解决方案1】:

    我收到了此代码的输出.....以下是我的代码片段。

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

    【讨论】:

      猜你喜欢
      • 2017-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-30
      • 2017-01-14
      • 2018-03-10
      • 2014-09-24
      相关资源
      最近更新 更多