0

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 the grant_type, client_id, and client_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"];
?>
Community
  • 1
  • 1
Noizzy
  • 11
  • 1
  • can you show us some code samples of what you did? – boroboris Oct 17 '16 at 10:52
  • Welcome to SO. Please read [What topics can I ask about](http://stackoverflow.com/help/on-topic) and [How to ask a good question](http://stackoverflow.com/help/how-to-ask) And [the perfect question](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) And how to create a [Minimal, Complete and Verifiable example](http://stackoverflow.com/help/mcve) – RiggsFolly Oct 17 '16 at 10:54
  • Hello yes I edited it. And sorry i'm new here. – Noizzy Oct 17 '16 at 10:56

0 Answers0