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

[英]Correct way to set Bearer token with CURL


I get my bearer token from an API end point and set the following:

我从API端点获取持有者令牌并设置以下内容:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

Next I want to use CURL to access the secure endpoint however I am unsure on how or where to set the Bearer token.

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

I have tried this but but it does not work:

我试过这个,但它不起作用:

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

EDIT:

编辑:

According to the documentation, I am supposed to be using the bearer token as such: https://apigility.org/documentation/auth/authentication-oauth2

根据文档,我应该使用持票人令牌:https://apigility.org/documentation/auth/authentication-oauth2

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

4 个解决方案

#1


45  

Replace:

更换:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

with:

有:

$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274";

to make it a valid and working Authorization header.

使其成为有效且有效的授权标头。

#2


1  

This is a CURL function I use to send/retrieve data between my apps, which should work OK within any PHP app that supports oAuth

这是我用来在我的应用程序之间发送/检索数据的CURL函数,它可以在任何支持oAuth的PHP应用程序中正常工作

    function jwt_request($token, $post) {

       $ch = curl_init('https://APPURL.com/api/json.php'); // INITIALISE CURL

       header('Content-Type: application/json');
       $post = json_encode($post); // Create JSON string from data ARRAY
       $authorization = "Authorization: Bearer ".$token; // **Prepare Autorisation Token**
       curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization )); // **Inject Token into Header**
       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);

    }

Use it as one way or a 2-way requests:

将其用作单向或双向请求:

$token = "080042cad6356ad5dc0a720c18b53b8e53d4c274"; // GET YOUR TOKEN FROM A COOKIE OR DB
$post = array('some_trigger'=>'...','some_values'=>'...'); // ARRAY OF DATA WITH A TRIGGER
$request = jwt_request($token,$post); // - To pass/retrieve data

#3


0  

If you are working with a private token instead (like Gitlab API), you should replace:

如果您正在使用私有令牌(如Gitlab API),则应替换:

$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"

$ authorization =“授权:Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274”

with:

有:

$authorization = "PRIVATE-TOKEN 080042cad6356ad5dc0a720c18b53b8e53d4c274";

$ authorization =“PRIVATE-TOKEN 080042cad6356ad5dc0a720c18b53b8e53d4c274”;

#4


0  

This should works

这应该有效

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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2015/05/24/3a691a6ba1fbb9e714ca299e8cb4708d.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号