Следующий код из других сообщений: Twitter 1.1 OAuth authenticity_token_error (99) , https://gist.github.com/lgladdy/5141615 <- Спасатель и другие. </p>
Я убедился, что:- Я вышел из Твиттера.- Запрос содержит все, что ему нужно.- У меня CORS включен через плагин Chrome.- Ключи в кодировке Base64 совпадают.
Я все еще получаю «код»: 99, «сообщение»: «Невозможно подтвердить свои учетные данные».Я могу воспроизвести эту ошибку 99 при изменении любого параметра в запросе с CURL и PHP.Так что что-то должно быть не так с кодом React.Я не смог найти рабочую версию кода на javascript.
Завиток работает:
curl --request 'POST' 'https://api.twitter.com/oauth2/token' --header 'Authorization: Basic ENCODED_KEY+ENCODED_SECRET, Content-Type: "application/x-www-form-urlencoded;charset=UTF-8"' --data "grant_type=client_credentials" --verbose
PHP - документ работает:
<?php
//This is all you need to configure.
$app_key = 'KEY';
$app_token = 'SECRET';
//These are our constants.
$api_base = 'https://api.twitter.com/';
$bearer_token_creds = base64_encode($app_key.':'.$app_token);
//Get a bearer token.
$opts = array(
'http'=>array(
'method' => 'POST',
'header' => 'Authorization: Basic '.$bearer_token_creds."\r\n".
'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
'content' => 'grant_type=client_credentials'
)
);
$context = stream_context_create($opts);
$json = file_get_contents($api_base.'oauth2/token',false,$context);
$result = json_decode($json,true);
if (!is_array($result) || !isset($result['token_type']) || !isset($result['access_token'])) {
die("Something went wrong. This isn't a valid array: ".$json);
}
if ($result['token_type'] !== "bearer") {
die("Invalid token type. Twitter says we need to make sure this is a bearer.");
}
//Set our bearer token. Now issued, this won't ever* change unless it's invalidated by a call to /oauth2/invalidate_token.
//*probably - it's not documentated that it'll ever change.
$bearer_token = $result['access_token'];
//Try a twitter API request now.
$opts = array(
'http'=>array(
'method' => 'GET',
'header' => 'Authorization: Bearer '.$bearer_token
)
);
$context = stream_context_create($opts);
$json = file_get_contents($api_base.'1.1/statuses/user_timeline.json?count=1&screen_name=lgladdy',false,$context);
$tweets = json_decode($json,true);
echo "@lgladdy's last tweet was: ".$tweets[0]['text']."\r\n";
echo $bearer_token_creds;
?>
Ошибка React 99:
var R = require('request'); //default React
var consumer_key = 'KEY';
var consumer_secret = 'SECRET';
var encode_secret = new Buffer(consumer_key + ':' + consumer_secret).toString('base64');
console.log('encode_secret', encode_secret)
R({
url: 'https://api.twitter.com/oauth2/token',
method: 'POST',
header: {
'Authorization': 'Basic ' + encode_secret,
'Content-Type': "application/x-www-form-urlencoded;charset=UTF-8"
},
content: "grant_type=client_credentials"
}, function (err, resp, body) {
console.log("B1", body); // <<<< This is your BEARER TOKEN !!
console.log("R1", resp);
console.log("E1", err);
});
Может кто-нибудь помочь?