I'm using Postman to obtain the access token as per the documentation provided by Hootsuite. The process involves requesting to authorize endpoint with the required parameters (client_id
, response_type=code
, redirect_uri
and scope
).
Once the authentication is done, Hootsuite will send access_token
and refresh_token
.
The provided access token has only 60 minutes expiry time. Therefore, it is required to refresh the token frequently.
As per their documentation to refresh the workflow, a server-to-sever request should be done where the refresh_token
should be sent in the request to obtain a new access_token
(that to be used in the regular calls like scheduling messages) and a new refresh_token
that would be used again to get a further newer token and so on.
My code to do that is as follows (comments in the code explain the steps):
<?php
$api = 'https://platform.hootsuite.com/oauth2/token';//endpoint
//parameters passed below
$data = array(
'grant_type' => 'refresh_token',
'refresh_token' => 'refresh_token_obtained_during_the_authorization',
'scope' => 'offline'
);
$payload = json_encode($data);//convert parameters to json
//header sent below
$header = array(
'client_id: my_client_id_xxxxxxx',
'client_secret: my_client_seceret_xxxxxx',
'Content-Type: application/json',
'Content-Length: ' . strlen($payload)
);
$ch = curl_init($api);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");//set to post.
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);//send the parameters.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//expect respond.
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//send headers.
$result = json_decode(curl_exec($ch));//execute
print_r ($result);//get responce
?>
The problem is although I have followed carefully the documentations. However, I keep receiving error message 400 as follows:
stdClass Object ( [error] => invalid_request [error_description] => The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed [error_hint] => The POST body can not be empty. [status_code] => 400 )
I appreciate if you could help me find the mistake in the code or the process itself.