Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to set Bearer token with CURL

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

$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.

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

GET /oauth/resource HTTP/1.1 Accept: application/json Authorization: Bearer 907c762e069589c2cd2a229cdae7b8778caa9f07 
like image 613
HappyCoder Avatar asked May 24 '15 17:05

HappyCoder


People also ask

How do you give Bearer Token in curl?

Sending the Bearer Token with a Curl POST request is similar to sending the Bearer Token with a Curl GET request. POST data is passed with the -d command-line option, and the authorization header and the bearer token are passed with the -H command-line option.

How do you set a Bearer Token?

Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type dropdown list. In the Token field, enter your API key value.

What is the format of Bearer Token?

Optional bearerFormat is an arbitrary string that specifies how the bearer token is formatted. Since bearer tokens are usually generated by the server, bearerFormat is used mainly for documentation purposes, as a hint to the clients. In the example above, it is "JWT", meaning JSON Web Token.


2 Answers

Replace:

$authorization = "Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274" 

with:

$authorization = "Authorization: Bearer 080042cad6356ad5dc0a720c18b53b8e53d4c274"; 

to make it a valid and working Authorization header.

like image 148
Hans Z. Avatar answered Oct 11 '22 18:10

Hans Z.


This is a cURL function that can send or retrieve data. It should work with any PHP app that supports OAuth:

    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      } 

Use it within one-way or two-way requests:

$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 
like image 30
SergeDirect Avatar answered Oct 11 '22 18:10

SergeDirect