Unable to get tokens for app using code from azure graph - php

I have created an app in http://apps.dev.microsoft.com/ and tried to connect from my web application
Here is my code:
$data = array (
'code' => $code,
'client_secret' => 'C2A32632155A3270220244A5774431C58126F9B5',
'client_id' => '49c1c823-b423-4673-af57-7be1ab39e386',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://localhost/crm/contacts/connectOffice',
'scope' => 'offline_access Contacts.ReadWrite'
);
$url = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($curl);
I get the response :
[error] => invalid_client
[error_description] => AADSTS70002: Error validating credentials. AADSTS50012: Invalid client secret is provided. Trace ID:
47f5eaa3-2ea0-45bc-9bfa-8457395ae354 Correlation ID:
3007e67d-120d-4cf1-a0e6-1863d202b233 Timestamp: 2017-01-12 13:12:28Z
[error_codes] => Array
(
[0] => 70002
[1] => 50012
)
[timestamp] => 2017-01-12 13:12:28Z
[trace_id] => 47f5eaa3-2ea0-45bc-9bfa-8457395ae354
[correlation_id] => 3007e67d-120d-4cf1-a0e6-1863d202b233
I am quite sure that I have provided the correct client secret and client Id in the request but still I get this error all the time . Can anyone suggest where I may be going wrong or what more do I need to do to correct this ?

The value you're using in your client secret field right now is actually a Public Key that is generated if you hit the Generate New Key Pair button.
For what you're trying to do, you'll want to hit the Generate New Password button in the portal to get a 23 character client secret. That should allow you to get an auth code.
One other thing, make sure if you're trying to get an auth code you use the correct authorization endpoint https://login.microsoftonline.com/common/oauth2/v2.0/authorize. The url you have above is used when you have an auth code and want to exchange it for tokens.

Related

Unable to get oAuth token for BigCommerce once click app

I am a complete noob to BigCommerce as well as plugins. And recently planned to develop a one click app which could be installed by the users of any store on their store. I have good hand on php. So, i can code but dont know the flow for plugins.
To create a draft app i went to devtools.bigcommerce.com and created an app defined Auth Callback url as https://amanangira.com/bCommerce/oauth.php
and Load Callback url as https://amanangira.com/bCommerce/callBack.php
in oauth.php i inserted the following code
<?php
$data = array( "client_id" => "123456789",
"client_secret" => "123456789",
"redirect_uri" => "https://amanangira.com/bCommerce/callBack.php",
"grant_type" => "authorization_code",
"code" => $_GET["code"], "scope" => $_REQUEST["scope"], "context" => $_GET["context"], );
$postfields = http_build_query($data);
$ch = curl_init();
//$url = "https://api.bigcommerce.com/stores/wky4s3lfef/v3/";
$url = "https://login.bigcommerce.com/oauth2/token";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec ($ch);
curl_close ($ch);
$obj = json_decode($output);
var_dump($obj);
?>
Now whenever i install the app in my test store it says following.
/var/www/html/angira/bCommerce/oauth.php:37:
object(stdClass)[3]
public 'error' => string 'redirect_uri_mismatch' (length=21)
public 'error_description' => string 'Parameter redirect_uri does not match registered URI' (length=52)
Please help with the above in as much as simple terms.
Thank you.
After some hit and trials and i finally stumbled upon the solution. Which was to update the redirect_uri in the oauth.php to that which was the Auth Callback uri in the application configuration.
"redirect_uri" => "https://amanangira.com/bCommerce/oauth.php"

403 forbidden error while sending messages to facebook connector through Unification Engine API

I am using unification engine #unificationengine API to post message on facebook.
I followed all the steps and created connections to use connectors. All the curl requests are working fine till send message.
In every curl from create user, create connection, connection refresh I am getting
{'status':200,'info':'ok'}
And now I want to use the connector to post message on facebook.
Below is my Curl code:
$post_msg = json_encode(
array(
'message' =>
array(
'receivers' =>
array(
array(
'name' => 'Me',
'address' =>'https://graph.facebook.com/'.$request->profile_id.'/feed?access_token='.$request->access_token.'&message=Hello&method=post',
'Connector' => 'facebook'
),
),
'sender' =>
array('address' => 'sender address'),
'subject' => 'Hello',
'parts' =>
array(
array(
'id' => '1',
'contentType' => 'binary',
'data' => 'Hi welcome to UE',
'size' => 100,
'type' => 'body',
'sort' => 0
),
),
),
)
);
$ch = curl_init('https://apiv2.unificationengine.com/v2/message/send');
curl_setopt($ch, CURLOPT_USERPWD, "0a7f4444-ae4445-45444-449-d9b7daa63984:8755b446-6726-444-b34545d-713643437560");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_msg);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// execute!
$response = curl_exec($ch);
// close the connection, release resources used
curl_close($ch);
// do anything you want with your response
var_dump($response);
return ['label' => $response];
and I am getting:
status: 403 and info: forbidden in response.
I have tried everything available in documentation and on stack overflow or any other website. But hard luck.
Please suggest why I am getting this error?
Refrence SO Questions:
SO question 1
SO question 2
Thanks.
Update
I added these three options in curl request:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_VERBOSE, true);
and now I am getting 498, invalid access token error:
"{\"Status\":{\"facebook\":{\"status\":498,\"info\":\"Invalid Token:
\"}},\"URIs\":[] }
please use this as per php
public function facebookSharing($access_token) {
$app = new UEApp(env('UNIFICATION_APP_KEY'), env('UNIFICATION_APP_SECRATE'));
$user = new UEUser('unification_userkey', 'unification_usersecret');
$connection = $user->add_connection('FACEBOOK', "facebook", $access_token);
$options = array(
"receivers" => array(
array(
"name"=> "Me"
)
),
"message"=>array(
"subject"=>'testing',
"body"=> 'description',
"image"=> 'use any image url',
"link"=>array(
"uri"=> 'any web site url',
"description"=> "",
"title"=>"Title"
)
)
);
$uris = $connection->send_message($options);
}
The access token might have expired. Please reconnect the facebook connection again or refresh the connection.
The facebook access tokens have a lifetime of about two hours. For longer lived web apps, especially server side, need to generate long lived tokens. Long lived tokens generally lasts about 60 days.
UE has a capability to refresh facebook tokens. After adding connection using "apiv2.unificationengine.com/v2/connection/add"; api call, then you should call "apiv2.unificationengine.com/v2/connection/refresh"; api to make the short lived token to long lived.

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"

invalid_grant while trying to get the Access token request Azure AD

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.

Instagram Real time updates tag - getting empty data, why?

Heloo,
I am working on one project and I need real time updates from Instagram for certain tag.
This is my code for Create a Subscription
<?php
$client_id = 'MOJID';
$client_secret = 'MOJIDSECRET';
$redirect_uri = 'http://example.net/instagram/callback.php';
$apiData = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'aspect' => "media",
'object' => "tag",
'object_id' => "winter",
'callback_url' => $redirect_uri
);
$apiHost = 'https://api.instagram.com/v1/subscriptions/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiHost);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($apiData));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$jsonData = curl_exec($ch);
curl_close($ch);
var_dump($jsonData);
?>
And output is:
[meta] => stdClass Object
(
[code] => 200
)
[data] => stdClass Object
(
[object] => tag
[object_id] => winter
[aspect] => media
[callback_url] => http://example.net/instagram/callback.php
[type] => subscription
[id] => 3932963
)
)
This is code for callback.php:
<?php
if (isset ($_GET['hub_challenge'])){
echo $_GET['hub_challenge'];
}
else{
$myString = file_get_contents('php://input');
$ALL = $myString."\r\n";
file_put_contents('activity.log', $ALL, FILE_APPEND | LOCK_EX);
}
?>
And a line from activity.log is:
[{"changed_aspect": "media", "object": "tag", "object_id": "winter", "time": 1385411793, "subscription_id": 3932963, "data": {}}]
I am getting the right subscription_id but the data is empty.
Also Acces log looks like:
54.209.52.224 - - [25/Nov/2013:20:59:20 +0100] "POST /instagram/callback.php HTTP/1.0" 200 231 "-" "Python-httplib2/0.7.4 (gzip)"
And this is good, status code 200 but once again data is empty. Instagram is 'comming' to my callback file but the data is empty.
What am I doing wrong?
Instagram Realtime API subscription will only let you know when there has been an update to your subscribed object, not what the update is. Once you receive a notification, it is up to you to make a call to the API (in your case probably https://api.instagram.com/v1/tags/winter/media/recent ) and to see what the new content is.
Depending on the volume of changes, you will probably want to batch these calls up at certain time intervals, but the below should be a good start. You'll also probably only want to retrieve items you haven't already retrieved.
<?php
if (isset ($_GET['hub_challenge'])){
echo $_GET['hub_challenge'];
}
else{
$myString = file_get_contents('php://input');
$sub_update = json_decode($myString);
$access_token = '{ previously saved, get this from DB or flatfile }';
foreach($sub_update as $k => $v) // can be multiple updates per call
{
$recent_data = file_get_contents('https://api.instagram.com/v1/tags/'.$sub_update->object_id.'/media/recent?access_token='.$access_token);
file_put_contents('activity.log', serialize($recent_data->data), FILE_APPEND | LOCK_EX);
}
}
?>
I get the $_POST to say there is an update from one of authenticated users, problem is there are 153 of them subscribed to the feed. The only way to get check if a user has an update is to get their latest.
I also can post a photo on IG myself. The $_POST is received triggering the loop of users being polled but the newest item won't show up in the api at the same time as the $_POST trigger arrives from the real-time api.

Categories