一尘不染

使用CURL设置Bearer令牌的正确方法

php

我从API端点获取了承载令牌,并设置了以下内容:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

接下来,我想使用CURL访问安全端点,但是不确定如何或在何处设置Bearer令牌。

我已经尝试过了,但是没有用:

 curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    return json_decode($result);

编辑:

根据文档,我应该这样使用承载令牌:https : //apigility.org/documentation/auth/authentication-
oauth2

GET /oauth/resource HTTP/1.1
Accept: application/json
Authorization: Bearer 907c762e069589c2cd2a229cdae7b8778caa9f07

阅读 2779

收藏
2020-05-29

共1个答案

一尘不染

更换:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

与:

$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274";

使其成为有效且有效的Authorization标头。

2020-05-29