Exchange authorization code for tokens - HTTP ERROR 415 Unsupported Media Type

Drace
Member

Below PHP code to exchange authorization code for token, Please help on this:

 

$code = $_GET['code'];

$url
= "https://api.hubapi.com/oauth/v1/token";

$post_fields = array('form' =>
array(
'grant_type' => 'authorization_code',
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'redirect_uri' => REDIRECT_URI,
'code' => $code
)
);
$post_fields = json_encode($post_fields);

$headers = array(
"Accept:application/json",
"Content-Type:application/json",
);


$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_fields );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_HEADER, true);
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0 );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0 );

$page = curl_exec($ch);
$info = curl_getinfo($ch);

var_dump($page);
var_dump($info);
0 Upvotes
1 Reply 1
dennisedson
Community Manager

Hi @Drace ,

Wanted to point you to this post originally asked by @michaelbarley which includes some possibly helpful resources.

 

Hope you get this resolved!

 

Thanks,

Dennis




Check out our Community Developer Blog
where we feature our Community driven developer podcast and how to content
0 Upvotes