简体   繁体   中英

cURL POST Request with Authorization

I'm trying to use cURL POST to get some data from an endpoint:

Here is what the documentation says to authenticate and get data:

"...uses a combination of OAuth2 grant_types and JWT tokens

To authorize, use this code:

curl -X POST \
  'http://api.example.com/v1/api/auth/login?grant_type=client_id' \
  -H 'Authorization: Basic cHVibGljX2tleTpwddl2YXRJX2xleQzd'

"

Based on the info above, I built this code for the request:

$handle = curl_init('http://api.example.com/v1/api/auth/login? 
grant_type=client_id');
$header = array();
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic DdJfd1Bxx2NxMkYwNjzzdl9mejJZIFKVQlBc';
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $header);
$resp = curl_exec($handle);

var_dump($resp);

When I execute the code, it runs in an endless loop and eventually times out.

Is the format of the code correct or is it a problem with the authorization key I provided? The key is a Base64 representation.

Thanks!

UPDATE: I've tried the following also:

$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic adfsidfosfosfodsofs';
$content = "grant_type=client_id";

$curl = curl_init();

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($curl, CURLOPT_POSTFIELDS, $content);

maybe try this:

$handle = curl_init('http://api.example.com/v1/api/auth/login?grant_type=client_id');
$header = array();
$header[] = 'Content-Type: application/json';
$header[] = 'Authorization: Basic DdJfd1Bxx2NxMkYwNjzzdl9mejJZIFKVQlBc';
curl_setopt($handle, CURLOPT_HTTPHEADER, $header);
$resp = curl_exec($handle);
var_dump(curl_error($handle));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM