Disqus OAuth Invalid Grant - php

I keep getting this error when I try to use OAuth with Disqus:
{"error_description":"Invalid parameter: redirect_uri","error":"invalid_grant"}
My Code looks like this- Example 1:
$oauth2token_url = 'https://disqus.com/api/oauth/2.0/access_token/';
$redirect_uri = 'http://www.example.com/';
$clienttoken_post = array(
"grant_type" => 'authorization_code',
"client_id" => PVConfiguration::getConfiguration('disqus') -> public_key,
"client_secret" => PVConfiguration::getConfiguration('disqus') -> private_key,
"redirect_uri" => $redirect_uri,
"code" => $this -> registry -> get['code']
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
print_r($json_response);
Or this - Example 2:
$url = 'https://disqus.com/api/oauth/2.0/access_token/';
$fields = array(
'grant_type' => 'authorization_code',
'client_id' => PVConfiguration::getConfiguration('disqus') -> public_key,
'client_secret' => PVConfiguration::getConfiguration('disqus') -> private_key,
'redirect_uri' => 'http://www.example.com/',
//'scope' => 'read,write,email',
'code' => $this -> registry -> get['code'],
);
$fields_string = '';
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
print_r($result);
exit();
Can anyone provide any direction?

Figured it out. Documentation is here:
https://www.rfc-editor.org/rfc/rfc6749#section-4.1.3
But the problem stems from bad api error message. The redirect uri to get the code must be the exact same uri when requesting authorization.

Related

PHP - How to get OAuth 2.0 Token

I want to connect to OAuth 2.0 authorization type (with password grant_type) to get token with POST method, it's ok to test from Postman but i couldn't connect through PHP...
here's my information:
request url: 'http://example.com/core/connect/token'
ClientName: 'something1'
ClientId: 'something2'
Secret: 'something3'
UserName: 'something4'
Password: 'something5'
Scope: 'something6'
could you please just give me an example to get token and use?
i've already tested:
$base_url = 'https://example.com/oauth/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'client_id' => YOUR-CLIENT-ID,
'client_secret' => YOUR-CLIENT-SECRET,
'username' => YOUR-USERNAME-OR-EMAIL,
'password' => YOUR-PASSWORD,
'grant_type' => 'password'
));
$data = curl_exec($ch);
$auth_string = json_decode($data, true);
and this
$api = "KEY GOES HERE";
$authurl = "http://example.com/core/connect/token";
$client_id = "ID GOES HERE";
$client_secret = "SECRET GOES HERE";
// Creating base 64 encoded authkey
$Auth_Key = $client_id.":".$client_secret;
$encoded_Auth_Key=base64_encode($Auth_Key);
$headers = array();
$headers['Authorization'] = "Basic ".$encoded_Auth_Key;
$headers['Content-Type'] = "application/x-www-form-urlencoded";
$data = array(
'grant_type' => 'password',
'scope' => 'read write',
'username' => $api,
'password' => $api,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authurl);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$auth = curl_exec( $ch );
if ( curl_errno( $ch ) ){
echo 'Error: ' . curl_error( $ch );
}
curl_close($ch);
$secret = json_decode($auth);
$access_key = $secret->access_token;
echo $secret;
The first call seems OK for the password grant type. The scope parameter should be optional. So...
Add this to your code to know the response from the server and therefore know the missing parameter or setting.
curl_setopt($ch, CURLOPT_VERBOSE, true);
Check the status code and a possible body with an error message.

Unable to Exchange Uber API authorization code for an access token for fetching current trip details

Once the Uber user authenticates and authorizes my app, i am receiving an authorization code but i am unable to exchange authorization code for an access_token.
I am trying to fetch current trip info using :
https://developer.uber.com/docs/trip-experiences/references/api/v1-requests-current-get
// uber.php
echo $_GET['code']."<br>";
$token = curl_init();
$param = array(
'client_secret' => 'xxxxxxxxxxxxxx',
'client_id' => '_xxxxxxxxxxxxxxxxxxxxxx',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://localhost/uber/uber.php', // (the same as in app settings)
'code' => "{$_GET['code']}"
);
$postData = '';
foreach($param as $k => $v)
{
$postData .= $k . '='.urlencode($v).'&';
}
$postData = rtrim($postData, '&');
curl_setopt($token, CURLOPT_URL, 'https://login.uber.com/oauth/v2/token');
curl_setopt($token, CURLOPT_HEADER, true);
curl_setopt($token, CURLOPT_RETURNTRANSFER, true);
curl_setopt($token, CURLOPT_POST, true);
curl_setopt($token, CURLOPT_POSTFIELDS, $postData);
$returned_token = curl_exec($token);
curl_close($token);
echo "<hr>";
echo $returned_token;
echo "<hr>";
I am getting a blank output.
addded this :
curl_setopt($token, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
and the problem was solved... :-)

Posting to Shopify using PHP

I'm trying to post from a PHP page to Shopify to indicate that an order item has been fulfilled. I must not be doing this correctly because I keep getting a null result. I'm doing this on a test order that was cancelled, but I would expect that I'd at least get some sort of non-null response if that were the problem. Here is my code:
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.
'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$data = array('fulfillment' =>
array(
'tracking_number' => $TRACKING_NUMBER,
'tracking_company' => 'USPS',
'tracking_url' => $TRACKING_URL,
'line_items' =>
array(
'id'=>$ITEM_ID,
),
),
);
$session = curl_init( $baseUrl );
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec($session);
curl_close($session);
$json = json_decode( $response, true );
var_dump($json);
You are sending a json in the POST section, are you sure this is what you really want to do? The post data should be more like this, sending data1=test1&data2=test2. source
<?php
$API_KEY = 'my-api-key';
$SECRET = 'my-shared-secret';
$PASS = 'my-shopify-password';
$STORE_URL = 'mydomain.myshopify.com';
$ITEM_ID = 'my-item-id';
$ORDER_ID = 'my-order-number';
$TRACKING_NUMBER = '12345';
$TRACKING_URL = 'http:\/\/test.com\/testurl';
$baseUrl = 'https://'.$API_KEY.':'.$PASS.'#'.$STORE_URL.'/admin/orders/'.$ORDER_ID.'/fulfillments.json';
$fields = array(
'lname' => urlencode($last_name),
'fname' => urlencode($first_name),
'title' => urlencode($title),
'company' => urlencode($institution),
'age' => urlencode($age),
'email' => urlencode($email),
'phone' => urlencode($phone)
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($session, CURLOPT_URL, $baseUrl);
curl_setopt($session, CURLOPT_POST, count($fields));
curl_setopt($session, CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
var_dump($result);
//close connection
curl_close($ch);
?>
If you really wants to send JSON via the POST fields, maybe you are missing the length of your data : source
curl_setopt($, , CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data))
);
Shouldn't you be using complete.json instead ??? I may be misunderstanding but it sounds like your trying to complete a pending fulfillment.
/admin/orders/#{id}/fulfillments/#{id}/complete.json
https://docs.shopify.com/api/fulfillment#complete
I found the problem. Shopify requires the line items to have an additional array, so instead of this:
'line_items' =>
array(
'id'=>$ITEM_ID,
)
It should be this:
'line_items' =>
array(
array(
'id'=>$ITEM_ID,
)
)

Create webhook-url in formstack via api using php

I want to create a webhook url for pitifuller form by id here is my code i don't know what is my mistake
define('CLIENT_ID', 'client_id');
define('CLIENT_SECRET', 'client_secret');
define('REDIRECT_URL', 'redirect url'); // for testing, use the URL to this PHP file.
define('AUTHORIZE_URL', 'https://www.formstack.com/api/v2/oauth2/authorize');
define('TOKEN_URL', 'https://www.formstack.com/api/v2/oauth2/token');
$ch = curl_init(TOKEN_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'grant_type' => 'authorization_code',
'client_id' => CLIENT_ID,
'redirect_uri' => REDIRECT_URL,
'client_secret' => CLIENT_SECRET,
'id' => 'id', // here is my id
'url' => 'web_hook_url' // here is my webhook url which i want to create
)));
// oauth2 contains the the access_token.
$oauth2 = json_decode(curl_exec($ch));
You did mistake in your CURL call, currently you are pass post data in GET form and you need to pass Like this(POST form/:id/webhook), and you can do this:
$ch = curl_init('https://www.formstack.com/api/v2/form/your-form-id/webhook');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $oauth2->access_token
));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array(
'id' => $_POST['id'],
'url' => 'http://www.getbravo.com/formstack/index.php',
'append_data' => '1'
)));
and you get response like this
$forms = json_decode(curl_exec($ch));
print '<pre>';
print_r($forms);
print '</pre>';

Unable to get access token from azure media services REST API using PHP

I have the following code in php:
define("TOKEN_URL", "https://wamsprodglobal001acs.accesscontrol.windows.net/v2/OAuth2-13");
$arrData = array(
'grant_type=client_credentials',
'client_id='.CLIENT_ID,
'client_secret='.urlencode(ACCESS_KEY),
'scope=urn%3aWindowsAzureMediaServices'
);
$arrHeader = array(
'Content-length:'.strlen($this->generateData($arrData))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TOKEN_URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateData($arrData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$arrToken = json_decode($data);
I am unable to get the token code. Please can anyone check what could be wrong?
There could be a few issues:
You could simplify a few things and use http_build_query():
$data = http_build_query(array(
'grant_type' => 'client_credentials',
'client_id' => CLIENT_ID,
'client_secret' => ACCESS_KEY,
'scope' => 'urn:WindowsAzureMediaServices',
));
$ch = curl_init(TOKEN_URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (($res = curl_exec($ch)) === false) {
die(curl_error($ch));
}
$arrToken = json_decode($res);
If there's an error, the first thing to make sure is whether you have an updated list of CA certificates.

Categories