I'm trying to get some jSon objects from URL. But there is OAuth 2.0 which i can't pass through. I found this how to get json data but i can't find how to get through this authentication. This is what the api wants me to do:
"To obtain an access token using a client_credentials grant, send a
POST
request to the /oauth/access_token endpoint with thegrant_type
,client_id
, andclient_secret
parameters. If successful, it will return a Session object."
The URL is e.g. www.example.com/a1/ I found some tutorials how to POST via PHP, but I was doing it wrong probably.
Here is what i tried
<?php
$endpoint = "www.example.com/a1/oauth/access_token";
$params = array(
"client_id" => "...",
"client_secret" => "...",
"grant_type" => "client_credentials");
$curl = curl_init($endpoint);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HEADER,'Content-Type: application/x-www-form-urlencoded');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$postData = "";
//This is needed to properly form post the credentials object
foreach($params as $k => $v)
{
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
echo "Performing Request...";
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// evaluate for success response
if ($status != 200) {
throw new Exception("Error: call to URL $endpoint failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl) . "\n");
}
curl_close($curl);
$json_data = json_decode($json_response, true);
echo "My token: ". $json_data["access_token"];
?>