在C中使用LIBCURL来实现带有Bearer Token授权的GET调用



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

但是当我在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);

这是我的代码片段,

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: %sn", 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"))

我收到了此代码的输出。。。。。下面是我的代码段。

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

最新更新