Twitter 1.1 OAuth authenticity_token_error(99)
Asked Answered
M

5

9

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?

Mastodon answered 20/4, 2014 at 13:33 Comment(1)
Well, most likely the credentials are not right and therefore they could not be authenticated?Maser
P
20

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.

Privily answered 19/8, 2015 at 0:44 Comment(2)
You save my life.Selfexpression
OMFG.... WHYYYYYYYY?!?! I thought I was being rate limited or something.(Thanks for this!)Dishonorable
D
6

I struggled with this for awhile and none of the answers I've found seemed to help. The documentation for the error is also a vague "something went wrong".

My problem is that I was using a mashup of code I found, and the headers weren't used correctly:

$headers = array(
    'Authorization' => 'Basic ' . base64_encode($appid . ':' . $secret), // WRONG!!!

    'Authorization: Basic ' . base64_encode($appid . ':' . $secret),     // Correct!
    'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',     // Correct!
);

For me, the problem was that the Authorization header was using key-value format, while the content-type header was not. This broke the authorization header.

Here are some other things to check that also relate to error 99:

  1. Verify that your credentials are correct and base64 encoded (see above)
  2. Make sure the request is using POST
  3. Ensure the content-type is set (see above)
  4. Make sure you included grant_type=client_credentials as a post field.
  5. SSL is required, make sure that is being used (https://)
  6. Try verbose logging to help debugging. It should include SSL certificate information, your authorization header, and content type header. This won't show the grant_type field though, only headers.
  7. If everything looks OK but it still won't work, you might be getting rate limited. Rate limits reset every 15 minutes.

When you finally get your access token, make sure you cache it to avoid rate limiting. You get 450 requests every 15 minutes, I believe. Half of that will be spent on getting your access token if you don't cache it!

Dentoid answered 9/9, 2015 at 19:42 Comment(1)
Personally I was missing the 'Basic ' before the encoded key+secretNicotinism
Q
2

There's an accepted answer here already but just in case someone stroll to this post and had the same issue I did...

Twitter docs for reference -> OAuth 2.0 docs

Misconception #1: The Authorization String is generated using the consumer key (aka API-Key) and consumer secret (aka API Secret Key). The display of those credentials in the UI on developer.twitter.com is less apparent than that of apps.twitter.com. Nonetheless common RIF problem.

Misconception #2: This one is not really an misconception but an implementation error when base64 encoding the url concatenated Consumer Key+":"+Consumer Secret. If not doing this programmatically be sure to check for whitespaces anywhere (especially around the :) in the concatenated string you are base64 encoding.

Just a tad bit advice as well postman has a wonderful utility that makes the rest call to retrieve an oauth2.0 token (as well as other auth tokens) this was useful for me when trying to consume api's with the that required an oauth1.0 token

Querist answered 29/9, 2018 at 22:28 Comment(1)
I was stuck on this for like an hour, thanks for helpChurr
D
0

After fighting with this problem, i finally come up with the solution. Twitter is not actually sending the right message if error exist anywhere.

When i send request from curl, it works fine but when through code. i was having same error {"errors":[{"label":"authenticity_token_error","code":99,"message":"Unable to verify your credentials"}]}

So what i got, problem was lying with Access control header. setting these header does not work for me

     xhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
     xhttp.setRequestHeader('Access-Control-Allow-Headers', '*');
     xhttp.setRequestHeader('Access-Control-Allow-Origin', '*')
     xhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest')

as a workaround i just used this url to bypass request to handler cors

https://cors-anywhere.herokuapp.com/https://api.twitter.com/oauth2/token

added "https://cors-anywhere.herokuapp.com/" before the actual url and it began to work. hope someone may face this issue in problem

Doglike answered 9/9, 2016 at 7:43 Comment(0)
E
0

Twitter OAuth 2.0 Bearer Token:

Step 1: Encode consumer key and secret
A - Concatenate the encoded consumer key, a colon character ":", and the encoded consumer secret into a single string.
B - Base64 encode the string from the previous step.
Example function: convertStringBase64("Api key" +":"+"Api key secret")
C- This steps generate you "Authorization"

Step 2: Obtain a Bearer Token
URL: https://api.twitter.com/oauth2/token
The request must be an HTTP POST request.
The request must include an Authorization header with the value of Basic <base64 encoded value from step 1>.
The request must include a Content-Type header with the value of application/x-www-form-urlencoded;charset=UTF-8.
The body of the request must be grant_type=client_credentials.

Example request (Authorization header has been wrapped):

POST /oauth2/token HTTP/1.1
Host: api.twitter.com
User-Agent: My Twitter App v1.0.23
Authorization: Basic eHZ6MWV2RlM0d0VFUFRHRUZQSEJvZzpMOHFxOVBaeVJn
                     NmllS0dFS2hab2xHQzB2SldMdzhpRUo4OERSZHlPZw==
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
Content-Length: 29
Accept-Encoding: gzip

grant_type=client_credentials

If the request was formatted correctly, the server would respond with a JSON-encoded payload:

Example response:

HTTP/1.1 200 OK
Status: 200 OK
Content-Type: application/json; charset=utf-8
...
Content-Encoding: gzip
Content-Length: 140

{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}

For more informtaion look this in Twitter Developer API: https://developer.twitter.com/en/docs/authentication/oauth-2-0/application-only

Expeditionary answered 17/5, 2022 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.