Office365 push notification API error: verification failed because the response body - php

I am referring to o365 API documentation for adding push notification of calendar event. I am getting below error-
Post>>
array:4 [▼
"#odata.type" => "#Microsoft.OutlookServices.PushSubscription"
"Resource" => "https://outlook.office.com/api/v2.0/me/events"
"NotificationURL" => "https://mywebsite.com/notifications"
"ChangeType" => "Created,Updated"
]
Response>>
"{"error":{"code":"ErrorInvalidParameter","message":"Notification URL 'https://mywebsite.com/notifications?validationtoken=ODFkNDllYWEtMmExYi00NDVjLWJmNzUtOTBhZjg3MDAyNjhh' verification failed because the response body '\tsuccess\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000' is unexpected."}} ◀"
My code is as-
$post_data = [ "#odata.type"=> "#Microsoft.OutlookServices.PushSubscription",
"Resource"=> "https://outlook.office.com/api/v2.0/me/events",
"NotificationURL"=> 'https://mywebsite.com/notification',
"ChangeType"=> "Created,Updated",
// "expirationDateTime"=> "2018-05-09T18:23:45.9356913Z",
// "clientState"=> "testClientState"
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://outlook.office.com/api/v2.0/me/subscriptions",
//CURLOPT_URL => "https://graph.microsoft.com/beta/subscriptions",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode($post_data),
CURLOPT_HTTPHEADER => array(
// Set Here Your Requesred Headers
'Authorization: Bearer '.TOKEN,
'Content-Type: application/json',
'X-CSRF-TOKEN: TOKEN-XXXXX'
),
));
How can I resolve the error?
Thanks in advance.

Your notification endpoint must respond to a validation POST in a very specific way. Your response doesn't match that way, so the validation is failing.
From https://developer.microsoft.com/en-us/graph/docs/concepts/webhooks:
Microsoft Graph validates the notification URL in a subscription request before creating the subscription. The validation process occurs as follows:
Microsoft Graph sends a POST request to the notification URL:
POST https://{notificationUrl}?validationToken={TokenDefinedByMicrosoftGraph}
ClientState: {Data sent in ClientState value in subscription request (if any)}
The client must provide a response with the following characteristics within 10 seconds:
A 200 (OK) status code.
The content type must be text/plain.
The body must include the validation token provided by Microsoft Graph.
The client should discard the validation token after providing it in the response.

Related

zoho mail rest api POST

Here is the trouble. I have I cant seen to get it right. Any time I try to send a post to the api call.
https://www.zoho.com/mail/help/api/put-verify-domain.html
if shot me a error of JSON_PARSE_ERROR.
here is the body of the curl
[{"mode":"verifyDomainByCName"}]
curl header "Authorization: Zoho-oauthtoken $token", 'Content-Type: text/plain'
If I try this
{"mode":"verifyDomainByCName"}
I get a HTTP Status 415 – Unsupported Media Type
the base setting for curl
curl_setopt_array ( $this->handler , [
CURLOPT_URL => $this->url,
CURLOPT_HTTPHEADER => $this->header,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $this->data,
Thank you for any help
Karl K
Tried to run a script with a json body and keep getting error

Office365 OAuth - Token via CURL

I'm trying to connect to the Azure platform to grab a response, mainly to get the token for use when accessing an office365 mailbox
The following is what i'm using, but I always get a NULL response
What other CURLOPT_POSTFIELDS need to be included, or what else needs to be changed.
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://login.microsoftonline.com/".$tennantid."/oauth2/v2.0/authorize",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'client_id='.$appid.'&response_type=token&scope=https://graph.microsoft.com/User.Read&redirect_uri='.$uri,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
$response_decode = json_decode($response);
var_dump($response_decode);
curl_close($curl);
I currently get the token back ok when I use the following method
$params = array ('client_id' =>$appid,
'redirect_uri' =>$uri,
'response_type' =>'token',
'response_mode' =>'form_post',
'scope' =>'https://graph.microsoft.com/User.Read',
'state' =>$_SESSION['state']);
header ('Location: '.$login_url.'?'.http_build_query ($params));
Which works fine.
But I need to do CURL method as I need this running background cron job task
What do I seem to be missing?
Thanks in advance
A link to the API documentation may have helped. Without that I cannot help much. Just one obvious thing.
You are using Content-Type: application/x-www-form-urlencoded
You are not encoding the data. From MDN:
application/x-www-form-urlencoded: the keys and values are encoded in
key-value tuples separated by '&', with a '=' between the key and the
value. Non-alphanumeric characters in both keys and values are percent
encoded: this is the reason why this type is not suitable to use with
binary data (use multipart/form-data instead)
Source: MDN Post data
This would mean your request data should look like this:
client_id%3D1234%26response_type%3Dtoken%26scope%3Dhttps%3A%2F%2Fgraph.microsoft.com%2FUser.Read%26redirect_uri%3Dhttp%3Aexample.com
Try this:
$post = urlencode('client_id='.$appid.'&response_type=token&scope=https://graph.microsoft.com/User.Read&redirect_uri=http:example.com');
CURLOPT_POSTFIELDS => $ post,

Unable to POST data TO REST API Using PHP CURL but it work fine with Postman

I am trying to post a data to REST API using PHP/CURL and seems it's not working, as i get 301 Moved Permanently error,But it works fine with Postman. Here is the snippet of PHP CURL which i generated form Postman
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'api url',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('text' => 'test'),
CURLOPT_HTTPHEADER => array(
'api_key: key',
'Content-Type:application/json',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
And it always return 301 Moved permanently, Note if i change the API key i got Unauthorised error, which means it hits the server, but i am not sure what i am missing, I have tried with multiple headers combinations.
Any help on this regard would be highly appreciated.
Quoting the PHP manual :
CURLOPT_POSTFIELDS: This parameter can either be passed as a urlencoded
string like 'para1=val1&para2=val2&...' or as an array with the field
name as key and field data as value. If value is an array, the
Content-Type header will be set to multipart/form-data.
Since you use array you'll need to use multipart.
See this post : curl POST format for CURLOPT_POSTFIELDS

discord oauth2 access token api php returns "invalid user"

my project wants to authorize a user through Discord Api, user logs in to my project then redirected to discord authorization page that authorization renders a code and after that I need to get access token from code through token uri mention in Discord Developer Source, my code to access token is this:
$authorization = base64_encode("OAUTH2_CLIENT_ID:OAUTH2_CLIENT_SECRET");
$header = array("Authorization: Basic {$authorization}", "Content-Type: application/x-www-form-urlencoded");
$content = "grant_type=authorization_code&code=$authorization_code&redirect_uri=$redirectURI";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $tokenURL,
CURLOPT_HTTPHEADER => $header,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $content
));
$response = curl_exec($curl);
curl_close($curl);
it returns
{"error": "invalid_client"}
i checked same error was posted in here, which suggests to put application/x-www-form-urlencoded in header, my function is already doing it, I am using Discord for first time.
Any help would be appreciated.

POST using cURL and x-www-form-urlencoded in PHP returning Access Denied

I have been able to use the Advanced Rest Client Extension for chrome to send POST queries to an specific HTTPS server and I get Status Code: 200 - OK with the same body fields as the ones I used in this code, but when I run the following code I get this response: 403 - Access Denied.
<?php
$postData = array(
'type' => 'credentials',
'id' => 'exampleid',
'secret_key' => 'gsdDe32dKa'
);
// Setup cURL
$ch = curl_init('https://www.mywebsite.com/oauth/token');
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_POSTFIELDS => json_encode($postData)
));
// Send the request
$response = curl_exec($ch);
var_dump($response);
// Check for errors
if($response === FALSE){
die(curl_error($ch));
}
// Decode the response
$responseData = json_decode($response, TRUE);
// Print the date from the response
echo $responseData['published'];
?>
I've noticed as well that when I use Advanced Rest Client Extension for chrome and if I set the Content-Type to application/json I have to enter a login and a password that I don't know what are those because even if I enter the id and secret key that I have in the code it returns 401 Unauthorized. So I'm guessing this code that I wrote is not forcing it to the content-type: application/x-www-form-urlencoded, but I'm not sure. Thank you for any help on this issue!
Can you try like that and see if it helps:
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_COOKIEFILE => 'cookie.txt',
CURLOPT_COOKIEJAR => 'cookie.txt',
CURLOPT_USERPWD => 'username:password', //Your credentials goes here
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
CURLOPT_POSTFIELDS => http_build_query($postData),
));
I guess the site expect simple authentication on top of the secret_key that you already provided.
Also it is possible to send a Cookie, so just in case it is good idea to store it and use it again in the next Curl calls.

Categories