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


81

我从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

这是PHP吗?服务器如何期望发送此令牌?标头?

嗨-是的,这是PHP,通常将不记名令牌设置为标头。
HappyCoder

标头的名称是什么?

我从文档中添加了一个编辑。
HappyCoder

Answers:


115

更换:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

与:

$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274";

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


嗨-这是我尝试过的方法,但是存在相同的问题。我怀疑这个问题可能与Apigility更新有关,因为我也无法在邮递员身上使用它。
HappyCoder

通过将ApiGility回滚到以前的稳定版本来解决此问题。感谢您的意见,尽管不是我的解决方案,但它是其他有类似问题的解决方案,的确使我走上了正轨。谢谢您的意见!
HappyCoder


36

这是一个cURL函数,可以发送或检索数据。它应与任何支持OAuth的PHP应用程序一起使用:

    function jwt_request($token, $post) {

       header('Content-Type: application/json'); // Specify the type of data
       $ch = curl_init('https://APPURL.com/api/json.php'); // Initialise cURL
       $post = json_encode($post); // Encode the data array into a JSON string
       $authorization = "Authorization: Bearer ".$token; // Prepare the authorisation token
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // Inject the token into the header
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       curl_setopt($ch, CURLOPT_POST, 1); // Specify the request method as POST
       curl_setopt($ch, CURLOPT_POSTFIELDS, $post); // Set the posted fields
       curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // This will follow any redirects
       $result = curl_exec($ch); // Execute the cURL statement
       curl_close($ch); // Close the cURL connection
       return json_decode($result); // Return the received data

    }

在单向或双向请求中使用它:

$token = "080042cad6356ad5dc0a720c18b53b8e53d4c274"; // Get your token from a cookie or database
$post = array('some_trigger'=>'...','some_values'=>'...'); // Array of data with a trigger
$request = jwt_request($token,$post); // Send or retrieve data

有人投了反对票,请在下面的评论中详细说明。
SergeDirect

2
谢谢Serge ...像这样的电话苦了太久。所有文档都说“使用http_build_query()构建POST数组”。但是不起作用-我不知道这是否是OAuth的特性,但是您需要的是json_encode,如此处所示。虚拟啤酒已发送。
anoldermark

2
@anoldermark很高兴提供帮助。非常感谢您的赞许,确实有很大的不同。写下高质量的答案需要花费时间和精力,赞扬和正面评论令人鼓舞...写更多,写得好;)
SergeDirect

@SergeDirect,我们如何在文件APPURL.com/api/json.php中获取帖子值 以及如何进行jwt身份验证。请举一个例子。
akgola


11

这应该工作

$token = "YOUR_BEARER_AUTH_TOKEN";
//setup the request, you can also use CURLOPT_URL
$ch = curl_init('API_URL');

// Returns the data/output as a string instead of raw data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

//Set your auth headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json',
   'Authorization: Bearer ' . $token
   ));

// get stringified data/output. See CURLOPT_RETURNTRANSFER
$data = curl_exec($ch);

// get info about the request
$info = curl_getinfo($ch);
// close curl resource to free up system resources
curl_close($ch);


1

在PHP 7.3上:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BEARER);
curl_setopt($ch,CURLOPT_XOAUTH2_BEARER,$bearerToken);

0

如果您使用的是专用令牌(例如Gitlab API),则应替换:

$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

与:

$authorization = "PRIVATE-TOKEN 080042cad6356ad5dc0a720c18b53b8e53d4c274";


0
<?php
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "your api goes here",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer eyJ0eciOiJSUzI1NiJ9.eyJMiIsInNjb3BlcyI6W119.K3lW1STQhMdxfAxn00E4WWFA3uN3iIA"
  ),
 ));

$response = curl_exec($curl);
$data = json_decode($response, true);

echo $data;

?>
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.