invalid_grant while trying to get the Access token request Azure AD - php

I am trying to get the access token from Azure Ad as explained the below link. I need to use the office 365 REST API.
msdn.microsoft.com/en-us/library/azure/dn645542.aspx
Authorization code request is successfull and i am getting the code in return URL. But the Access token request is failing and the response i am getting as below.
stdClass Object
(
[error] => invalid_grant
[error_description] => AADSTS70002: Error validating credentials. AADSTS70000: The provided access grant is invalid or malformed.
Trace ID: baf9f98a-655d-48e1-bc30-021a7d57effa
Correlation ID: 5030b57f-42e2-403a-ab3c-cfc970d886e2
Timestamp: 2015-04-21 12:58:00Z
[error_codes] => Array
(
[0] => 70002
[1] => 70000
)
[timestamp] => 2015-04-21 12:58:00Z
[trace_id] => baf9f98a-655d-48e1-bc30-021a7d57effa
[correlation_id] => 5030b57f-42e2-403a-ab3c-cfc970d886e2
[submit_url] =>
[context] =>
)
Below is my code:
$url = "https://login.windows.net/<tenant-id>/oauth2/token";
$postUrlData = "grant_type=authorization_code&client_id=$clientId&code=$authCode&redirect_uri=$redirectUri&client_secret=$secretKey";
$headers = array(
'Content-type: application/x-www-form-urlencoded',
'Content-length: '. strlen($postUrlData)
);
//echo $postUrlData;die;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS , $postUrlData);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$res = json_decode($result);
curl_close($ch);
Can you please tell me what am i doing wrong?

Take a look at this PHP sample: https://github.com/jasonjoh/php-calendar. Specifically, look at the getTokenFromAuthCode function in https://github.com/jasonjoh/php-calendar/blob/master/o365/Office365Service.php.

Related

Calling google api url shortener via curl returns HTTP 403 status in php

I have a problem calling Google urlshortener. I use curl function to make a call:
$url = base_url()."home/register?source=#".$userid;
$longUrl = $url;
$apiKey = 'xxxxxxxxapikeyxxxxxxx';
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL,
'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-
type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
$json = json_decode($response);
curl_close($curlObj);
print_r($json);
So I'm getting the following response:
stdClass Object ( [error] => stdClass Object ( [errors] => Array ( [0] =>
stdClass Object ( [domain] => global [reason] => forbidden [message] =>
Forbidden ) ) [code] => 403 [message] => Forbidden ) )
Please help me.
You can no longer use the Google URL Shortener API using an api key.
Starting March 30, 2018, we will be turning down support for goo.gl URL shortener. Please see this blog post for detailed timelines and alternatives.
You should be aware that the Google URL Shortener API has been discontinued Transitioning Google URL Shortener to Firebase Dynamic Links
They have already begun tuning down a number of the features within the API. I suspect the issue you are having is due to that.
You should switch to FireBase Dynamic links

Curl Php Returns NULL in browser on

I am trying to authenticate to third party ticketing system, connect and retrieve agent info. I then want to parse retrieved file so as to only show specific info such as 'id' and' 'active_since', but I get an NULL error in browser. Any help?
FILE RETURNED BY THIRDPARTY
[agent] => Array
(
[active_since] => 2015-11-30T08:09:26-01:00
[available] => 1
[created_at] => 2015-05-14T19:15:18+00:00
[id] => XXXXX
[occasional] =>
[points] => 5520
[scoreboard_level_id] => 5000402007
[signature] =>
[signature_html] =>
Below is the PHP CURL code.
<?php
$username = "xxxxxxx";
$password = "xxxxxxx";
$url = 'http://support.xxxx.com/agents/xxx132067';
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'header' => "Authorization: Basic " . base64_encode("$username:$password")
)
);
$result = file_get_contents($url);
// Will dump a beauty json :3
var_dump(json_decode($result, true));
$array["active_since"];
?>
Remove the 'header' => from the Authorization header. And it should be as below:
"Authorization: Basic " . base64_encode("$username:$password")
And I didn't see you are calling curl at all. Replace your file_get_contents with proper curl call.
$result = curl_exec($cURL);
curl_close($cURL);

Neteller TransferOut with PHP / CURL

I'm having some problems using the Neteller API to transfer money out of our merchant account to a user. I've succcessfully received the accessToken, however when I try using transferOut I just get invalid credentials? The code I'm using is:
$headers = array(
"Content-type" => "application/json",
"Authorization" => "Bearer " . $accessToken
);
//build the request body structure
$requestParams = array(
"payeeProfile" => array(
"email" => $the_email_address_to_send_to
),
"transaction" => array(
"merchantRefId" => $transaction_id,
"amount" => $amount,
"currency" => $currencyCode
)
);
// encode the requestParams to a string
$requestParams = json_encode($requestParams);
// The curl stuff
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_URL, "https://api.neteller.com/v1/transferOut");
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $requestParams);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// Ok lets send this lovely looking curl over
$serverOutput = json_decode(curl_exec($curl));
Obviously all variables ($transaction_id, $amount, $currency) are set appropriately. However the response I get back is:
stdClass Object
(
[error] => stdClass Object
(
[code] => 5279
[message] => Authentication credentials are invalid
)
)
I'm confused, surely the accessToken is the credentials I need, and theyve already been received. Am I meant to include anything else in the transferOut curl postfields?
Thanks in advance
As per user3584460's comment:
$headers does not look OK - try $headers = array("Content-type: application/json", "Authorization: Bearer " . $accessToken);. At least this is the format according to http://php.net/manual/en/function.curl-setopt.php
Note, the Merchant Ref ID also needs to be a certain length. unsure what - can't find reference, but 8 characters is not long enough.

LMS API account creation resulting in an error

I am working on Canvas LMS and have access token. I need to create an user account using web service in PHP. I have tried to do it using CURL (post method) but getting an error in response. However GET is working fine.
Like if I need to get information about course etc, it's working fine but account creation not working using CURL (post). Below is my code.
$url = "https://xxxxx.com/api/v1/accounts/2/users";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' .$token ) );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'name' => 'vaue',
'short_name' => 'value',
'unique_id' => '1121',
));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);
Error:
stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[message] => An error occurred.
[error_code] => internal_server_error
)
)
[error_report_id] => 1124
)
I resolved my issue. The reason of "internal server error" was not sending required fields. Here are the required fields if someone need to know.
'user[name]' => '',
'user[terms_of_use]' => 'true',
'pseudonym[unique_id]' => '',//i.e valid email
'pseudonym[send_confirmation]'=>'true'
Now my CURL request is working fine and I am able to create an account successfully.
It looks like the keys for your arguments are incorrect. They should be:
'user[name]' => 'vaue',
'user[short_name]' => 'value',
'pseudonym[unique_id]' => '1121',
You can find the docs for your canvas install at: "https://{your canvas domain}/doc/api/index.html" or if you are using cloud hosted canvas at "api.instructure.com"

Goo.gl URL Shortener Stopped Working (php/curl)

For some reason my script stopped working today. When I look in the API control panel says I still have 100% left of usage. Any ideas? Did they change the auth way?
function url_small($url)
{
//This is the URL you want to shorten
$longUrl = $url;
$apiKey = '#####HIDDEN######';
//Get API key from : http://code.google.com/apis/console/
$postData = array('longUrl' => $longUrl, 'key' => $apiKey);
$jsonData = json_encode($postData);
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlObj, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curlObj, CURLOPT_HEADER, 0);
curl_setopt($curlObj, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($curlObj, CURLOPT_POST, 1);
curl_setopt($curlObj, CURLOPT_POSTFIELDS, $jsonData);
$response = curl_exec($curlObj);
//change the response json string to object
$json = json_decode($response);
curl_close($curlObj);
return $json->id;
}
Response
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[domain] => usageLimits
[reason] => dailyLimitExceededUnreg
[message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
[extendedHelp] => https://code.google.com/apis/console
)
)
[code] => 403
[message] => Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.
)
)
My below answer is no longer valid. Google now makes you use Firebase for URL Shortening. Easy to setup.
https://firebase.google.com/docs/dynamic-links/rest
So it turns out this old function that is displayed in multiple websites now needs the api key to be displayed in the URL section too for google to register the request to your account.
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url');
switched to this
curl_setopt($curlObj, CURLOPT_URL, 'https://www.googleapis.com/urlshortener/v1/url?key='.$apiKey);

Categories