Twitter OAuth要求两次确认

时间:2013-03-21 21:05:36

标签: php oauth twitter

我写了一个脚本,要求Twitter授权。如果脚本第一次运行,它将被重定向到Twitter,然后凭据将保存到json文件。这有效。但来自twitter的授权页面将被加载两次。也许有人可以帮我找到这个bug。这是脚本:

require('./vendor/autoload.php');

use TijsVerkoyen\Twitter\Twitter;

$twitter = new Twitter('xyz', 'xyz');
$response = $twitter->oAuthRequestToken('http://localhost:8888/tweeted-news/oauth-test.php');

$credentials = json_decode(file_get_contents('credentials.json'), true);

if (!isset($credentials['oauth_token'])) {
    $response = $twitter->oAuthAuthorize($response['oauth_token']);
    $response = $twitter->oAuthAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);
    file_put_contents('credentials.json', json_encode($response));
    var_dump('authorized');
} else {
    $twitter->setOAuthToken($credentials['oauth_token']);
    $twitter->setOAuthTokenSecret($credentials['oauth_token_secret']);
    var_dump('credentials');
}

var_dump($twitter->accountSettings());

我正在将这个库https://github.com/tijsverkoyen/TwitterOAuth与作曲家一起使用。非常感谢。

1 个答案:

答案 0 :(得分:1)

删除else行。在var_dump('authorized')之后,您应该设置OAuthToken和OAuthTokenSecret。所以代码应该是:

if (!isset($credentials['oauth_token'])) {
    $response = $twitter->oAuthAuthorize($response['oauth_token']);
    $response = $twitter->oAuthAccessToken($_GET['oauth_token'], $_GET['oauth_verifier']);
    file_put_contents('credentials.json', json_encode($response));
    var_dump('authorized');
    $twitter->setOAuthToken($credentials['oauth_token']);
    $twitter->setOAuthTokenSecret($credentials['oauth_token_secret']);
    var_dump('credentials');
}