Bearer Authorization denied in api using cURL - php

I want to make a simple post request to an API server. I know, there are lot of information about cURL in web, but I can't figure out, why am I getting:
[message] => Authorization has been denied for this request.
So the API uses standard bearer token(in the header Authorization: Bearer {tokenID})
my php code:
$body = array(
"id" => $uuid,
"userAccountId" => $userAccountId,
"email" => $email,
"first_name" => $first_name,
"last_name" => $last_name,
"phone" => $phone,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, http_build_query(array(
'Authorization' => $token,
)));
curl_setopt($ch, CURLOPT_URL, $myurl);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($body));
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result);
The most interesting and weird thing is, that, when I send a Post request to the same url with the same body and the same header(same token) using postman, it works, however it doesn't work with php cURL. Also I tried with https://www.codepunker.com/tools/http-requests and got the same Authorization error. Has anyone any ide what could it be ?

I found the issue: I should have used json_encode($body) instead http_build_query($body) and the header in the following way:
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer .....',
'accept: application/json',
'content-type: application/json',
));

Related

Pinterest 'grant_type' is a required property

Im trying to get access_token from pinterest api but getting this error.
'grant_type' is a required property
I'm using this login url:
https://www.pinterest.com/oauth/?client_id=APPID&redirect_uri=REDURL&response_type=code&scope=pins:write&state=eer
Redirect url have this code, which throws the error.
$vars = array(
"grant_type" => "authorization_code",
"code" => $_GET["code"],
"redirect_uri" => REDURL
);
$headers = [
'Authorization: Basic '.base64_encode("APPID:SECRET"),
'Content-Type: application/x-www-form-urlencoded'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://api.pinterest.com/v5/oauth/token");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$vars);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($ch);
curl_close ($ch);
print $result;
I'm trying to follow this: https://developers.pinterest.com/docs/getting-started/authentication/
Manual says about CURLOPT_POSTFIELDS,
if value is an array, the Content-Type header will be set to multipart/form-data.
That appears to be colliding with the application/x-www-form-urlencoded you are trying to send here.
Pass the data as a URL-encoded string instead, using http_build_query.

Understand api curl instruction

Stuck trying to get an api-connection to work. I believe I don't understand the below example request in the api. Especially the last row "grant_type..etc". How is this line to be handled in CURL? As POSTFIELDS? Get an error {"error":"unsupported_grant_type"}
POST /connect/token HTTP/1.1
Authorization: Basic base64(client_id:client_secret)
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
"grant_type=authorization_code&code=<authorization_code>&redirect_uri=<redirect_uri>"
Code so far:
$ch = curl_init('https://xxxx');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/x-www-form-urlencoded;charset=UTF-8',
'Authorization: Basic '.base64_encode($clientid.':'.$clientsecret).''
));
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $_GET['code'],
'redirect_uri' => $redirecturi,
'grant_type' => 'authorization_code'
));
$result = curl_exec($ch);
curl_close($ch);
echo $result;
https://auth0.com/docs/applications/reference/grant-types-available
Here all grant type are explained .

How do I add email address to MailChimp list using API

I am trying to use MailChimp API 3.0 to add an email address to existing list using this code below but it keeps throwing this error message. Please can anyone tell me what's wrong with my code?
401 : Your API key may be invalid, or you've attempted to access the
wrong datacenter.
Code:
$apikey = "xxxxxxxxxxxxxxxxxxxxxxxx-us9";
$list_id = "xxxxxxxxxx";
$MD5_hashed_email_address = md5(strtolower($to_email));
$auth = base64_encode("user:$apikey");
$data = array(
"apikey" => $apikey,
"email_address" => $to_email,
"status" => "subscribed",
"merge_fields" => array(
"FNAME" => $firstname,
"LNAME" => $surname
)
);
$json_data = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://us9.api.mailchimp.com/3.0/lists/$list_id/members/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json",
'Authorization: apikey ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, "PHP-MCAPI/2.0");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$result = curl_exec($ch);
Try this:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json",
'Authorization: Basic ' . $auth));

curl to php to post a json and audio

I'm trying to convert a curl command to be used in a php script
curl -k -F "request={'timezone':'America/New_York','lang':'en'};type=application/json" -F "voiceData=#d8696c304d09eb1.wav;type=audio/wav" -H "Authorization: Bearer x" -H "ocp-apim-subscription-key:x" "http://example.com"
and here is my php script
<?
$data = array("timezone" => "America/New_York", "lang" => "en", "voiceData" => "d8696c304d09eb1.wav");
$data_string = json_encode($data);
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer x',
'ocp-apim-subscription-key:x')
);
$result = curl_exec($ch);
?>
I understand that the send audio file bit is not right but i cant find an example how to post it.
I have edited this in response to the post fields but if I include $ch i get no output if don't include the output complains that no post request. Any ideas?
<?
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$ch = curl_init('https://example.com');
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/json',
'Authorization: Bearer x',
'ocp-apim-subscription-key:x')
);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
'voicedata' => new CURLFile("d8696c304d09eb1.wav")
)
);
$result = curl_exec($ch);
echo $result;
?>
You're missing the request= in your POST fields. Also, sending files using #filename is deprecated, you should use the CURLFile class.
curl_setopt($ch, CURLOPT_POSTFIELDS,
array('request' => json_encode(array('timezone' => 'America/New_York', 'lang' => 'en')),
'voicedata' => new CURLFile("d8696c304d09eb1.wav")
)
);

Pushbullet no notification on smartphone using API

I created a little app which uses Pushbullet's API.
It is writtent in PhP and uses cURL to proceed the different requests.
At first, I use oauth2 to recieve an access token.
Then it is /v2/pushes/ to send a push to the registred account.
The message is recieved on the Pusbullet account but nothing shows up on the smartphone. There is no notification.
I hope someone can help me.
Thanks.
My function which sends the push:
function send_push($token){
$data = array(
'type' => 'note',
'title' => 'Alert',
'body' => 'My body :)!'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.pushbullet.com/v2/pushes');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Authorization: Bearer '.$token
));
$data = curl_exec($ch);
curl_close($ch);
}

Categories