0

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

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和作曲家。非常感谢。

4

1 回答 1

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');
}
于 2013-03-22T16:50:18.137 回答