Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter 1.1 OAuth authenticity_token_error(99)

I use the following code to get the bearer token:

$token = base64_encode($client_id.':'.$client_sec);

$data = array ('grant_type' => 'client_credentials');
$data = http_build_query($data);

$header = array(
    'Authorization: Basic '.$token,
    'Content-type: application/x-www-form-urlencoded;charset=UTF-8',
    'Content-Length: ' . strlen($data)
);

$options = array(
    CURLOPT_HTTPHEADER => $header,
    CURLOPT_HEADER => false,
    CURLOPT_URL => 'https://api.twitter.com/oauth2/token',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POSTFIELDS => $data
);

$ch = curl_init();
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
curl_close($ch);
print_r($result);
exit;

But output all the time:

{"errors":[{"label":"authenticity_token_error","code":99,"message":"Unable to verify your credentials"}]}

What I doing wrong?

like image 602
Alexey Palamar Avatar asked Apr 20 '14 13:04

Alexey Palamar


People also ask

Does Twitter use OAuth?

Twitter allows you to obtain user access tokens through the 3-legged OAuth flow, which allows your application to obtain an access token and access token secret by redirecting a user to Twitter and having them authorize your application.

What is OAuth in Twitter API?

OAuth 1.0a allows an authorized Twitter developer App to access private account information or perform a Twitter action on behalf of a Twitter account.

How do I authenticate my Twitter account?

Now, when you log in to your account on twitter.com, Twitter for Android, or mobile.twitter.com, a six-digit code will be text messaged to your phone to use during login. Tap the checkbox next to Authentication app. Read the overview instructions, then tap Start. If prompted, enter your password and tap Verify.

How do I get my Twitter Bearer Token?

Login to your Twitter account on developer.twitter.com. Navigate to the Twitter App dashboard and open the Twitter App for which you would like to generate access tokens. Navigate to the "keys and tokens" page. You'll find the API keys, user Access Tokens, and Bearer Token on this page.


1 Answers

After fighting with this problem for a while I found the problem was I was making the call to /oauth2/token using Advanced Rest Client from a browser I was already logged into Twitter with. After logging out of Twitter and making the API call again it worked fine.

Short answer: make sure you do not already have an active session logged into Twitter when attempting to request a Bearer token.

like image 160
Phillip Avatar answered Oct 05 '22 22:10

Phillip