Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

php - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...