Twitter oAuth issue: Access token are not being retrieved i - php

I am getting following error while trying to fetch access_token:
The access_token method must be called with a request_token /oauth/access_token?oauth_version=1.0
Following is my code snippet:
require_once('OAuth_functions.php');
$objTwitter = new TwitterOAuth("xxxxx","xxxxxxxxxxxxxxxxxx","xxx-xxx","xxxxxx");
$access_token = $objTwitter->getAccessToken();
While printing the variable $access_token, following response retrieved:
Array
(
[ "1.0" encoding="UTF-8"?>
The access_token method must be called with a request_token
/oauth/access_token?oauth_version=1.0
[amp;oauth_nonce] => xxx
[amp;oauth_timestamp] => 1311491162
[amp;oauth_consumer_key] => xxx
[amp;oauth_token] => xxx-xxx
[amp;oauth_signature_method] => HMAC-SHA1
[amp;oauth_signature] => xxx=
)
I want to implement SIGN IN WITH TWITTER functionality for my site.

I think you need to use $_REQUEST['oauth_verifier'] via callback url.
check https://dev.twitter.com/discussions/7771

Related

How to register webhook url on Twitter app using Abraham library in PHP

I'm trying to register a webhook url on twitter app and I'm using this package twitteroauth.
But facing following error after executing Twitter endpoint to register webhook.
stdClass Object(
[errors] => Array
(
[0] => stdClass Object
(
[code] => 214
[message] => Webhook URL does not meet the requirements. Invalid CRC token or json response format.
)
)
)
I was tried to register webhook through code as follows:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_POST['oauth_token'], $_POST['oauth_token_secret']); $webhookURL = "https://example.com/twitter/webhook"; $ucontent = $connection->post('account_activity/all/my_env_name/webhooks', array('url' => $webhookURL));
After reading some posts regarding this issue, I was tried the following code with url encoding:
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $_POST['oauth_token'], $_POST['oauth_token_secret']); $webhookURL = "https://example.com/twitter/webhook"; $ucontent = $connection->post('account_activity/all/my_env_name/webhooks', array('url' => urlencode($webhookURL)));
I also read in some articles and regenerate the keys, tokens of my app and newly generated key, tokens in code.
But not get success to register webhook url.
Execute the Twitter endpoint to get the webhook related details, but its giving empty array
{ "environments": [ { "environment_name": "my-environment-name", "webhooks": [] } ] }
Expected output after successful webhook url registration in this array response:
{ "environments": [ { "environment_name": "my-environment-name", "webhooks": [ { "id" => webhook-id-here, "url" => https://example.com/twitter/webhook, "valid" => 1, "created_timestamp" => some-date-time } ] } ] }
Please help me for this issue.

OAuth.io server-side doesn't refresh Google token

At first please do not mark this as duplicated as this is related to server-side (PHP) and not to client-side as other posts.
I'm trying to refresh the token for Google API through Oauth.io. I followed this and many other posts but no luck. Unfortunately Oauth.io documentation is not the best one. I don't have this problem with Twitter and Facebook only Google.
I'm getting refresh_token when I connect for the first time. Then I need to do API call once a day.
{
"access_token":"xxx",
"token_type":"Bearer",
"expires_in":3600,
"refresh_token":"xxx",
"id_token":"xxx",
"provider":"google_analytics"
}
The question is how to refresh Google token through Oauth.io?
The documentation says:
// The auth method automatically refreshes the tokens if needed
$request_object = $oauth->auth('facebook', array(
'credentials' => $credentials
));
and points here, but it doesn't solve the problem. All it does is that I'm getting refresh_token value in response.
UPDATE
According to this post I tried to do:
$request_object->post('https://oauth.io/auth/access_token', array(
'code' => 'xxx', // here I tried access_token, refresh_token
'key' => 'xxx',
'secret' => 'xxx',
));
but all I'm getting is
array(4) {
'status' =>
string(5) "error"
'code' =>
int(401)
'message' =>
string(70) "Bearer token invalid. Follow the oauth2-token link to get a valid one!"
'data' =>
array(1) {
'code' =>
string(17) "UnauthorizedError"
}
}
Still nothing.
Finally I found it. You can refresh the access_token using:
$this->oauth = new OAuth();
$this->oauth->initialize($publicKey, $secretKey);
$refreshedCredentials = $this->oauth->refreshCredentials($oldCredentials, true);
or
$request_object = $oauth->auth('google_analytics', array(
'credentials' => $credentials,
'force_refresh' => true
));
Both answers can be found here. I had it in front of all day long and did not see it.
I prefer using 1st solution as I want to save new access_token so I can reuse it later. And you need to remember that you need to pass refresh_token, which you got with first access_token, along with last access_token to get refreshed access_token - refresh_token doesn't change until user revoke access.

Wordpress HTTP API with twilio

I am trying to send sms messages using Twilio API in Wordpress, but I don't understand what is curl -u. Now I want to send sms using twilio and wordpress via wp_remote_post.
See twilio send sms docs API (via JSON)
My code:
function sending_sms_via_twilio_api(){
$oauth_args = array(
"body" => array(
"Body" => "Hello World",
"To" => "0000000",
"From" => "5555555",
),
"my_Sid:my_token"
);
$response = wp_remote_post('https://api.twilio.com/2010-04-01/Accounts/AC28fcd041ffe3edb8029779894b7912d3/Messages.json', $oauth_args);
$result = json_decode( wp_remote_retrieve_body($response), true );
print_r($result);
}
Result is:
Array ( [code] => 20003 [detail] => Your AccountSid or AuthToken was
incorrect. [message] => Authentication Error - No credentials provided
[more_info] => [status] => 401 )
Any solutions?
That is a simple authentication problem. You should check your credentials again.
A GET call on https://{AccountSid}:{AuthToken}#api.twilio.com/2010-04-01/Accounts with wrong credentials give that problem.
Also the api response give this error page as advice for you:
https://www.twilio.com/docs/errors/20003
Hope it helps

BigCommerce OAuth "Invalid scope" error

I'm attempting to retrieve an access token from BigCommerce. I'm following the instructions on this page: https://developer.bigcommerce.com/apps/callback
When I try to retrieve the access token, I am getting an invalid scope error. Here's the code:
public function access_token_get(){
print_r($_GET);
$tokenUrl = "https://login.bigcommerce.com/oauth2/token";
$connection = new Connection();
$connection->setCipher('RC4-SHA');
$connection->verifyPeer(false);
$response = $connection->post($tokenUrl, array(
"client_id" => "123456",
"client_secret" => "123456",
"redirect_uri" => "https://my-registered-auth-callback.com/",
"grant_type" => "authorization_code",
"code" => urlencode($_GET['code']),
"scope" => urlencode($_GET['scope']),
"context" => urlencode($_GET['context'])
));
print_r($response);
print_r($connection->getLastError());
$token = $response->access_token;
print_r($token);
}
When this code runs, I get an empty $response. I added the getLastError() line to see what was going on, and it's outputting:
stdClass Object ( [error] => Invalid scope(s). )
These are the parameters output from the GET request:
Array ( [code] => 2idy1ozvee8s0ddlbg3jgquzgtr55gd [context] => stores/xxxxxx [scope] => store_v2_orders store_v2_products store_v2_customers store_v2_content store_v2_marketing store_v2_information_read_only users_basic_information )
Why would I be receiving this "invalid scopes" error? I also tried hardcoding a single scope to see if that works, for example, just doing "scope"=>"store_v2_orders", but when I do this, I get an error saying that the scope has not been granted by the user.
Looks like the issue was that I did not need to urlencode the code, scope, and context. Removing the urlencode function fixed the issue.

facebook getting session_key for offline_access in canvas application

Hey everyone,
I am trying to get the session_key and secret for a facebook session (offline_access) in canvas application.
However, I am unable to get these two particular variables.
When, I am testing the same code locally, I can get the variables above (but this can be because the local app is not a canvas application)
I 'm using the following code, for redirect:
$facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'user_status,publish_stream,offline_access'
));
Can someone suggest me on how I can grab the session_key and secret, when the user authorizes the application? (for permanent offline access)
Here is an example session dump via Facebook:
Array (
[uid] => 100000926583671
[session_key] =>
[expires] => 0
[secret] =>
[base_domain] =>
[access_token] => 183043495039366|3ab6ac2asdkhj1bcfdec13d7-100000926583671|jJQaIT-n80YxioAasdwN0cm99U
[sig] => 2f64sadasc1da31c12927a052752776
)
and this is the error:
Array (
[error] => Array (
[type] => OAuthException
[message] => An active access token must be used to query information about the current user.
)
)
session_key and secret are now deprecated, and Facebook wants you to use Oauth authentication schema. Then, you will have to use given access_token (that you correctly get in $session) to make your api calls.
Try:
$session = $facebook->getSession();
$me = $facebook->api('/100000926583671',
array('access_token' => $session['access_token'])
);
var_dump($me);
Ref:
http://developers.facebook.com/docs/authentication/

Categories