使用Twitter API刷新令牌

时间:2016-09-18 01:40:20

标签: twitter twitter-oauth

我想知道Twitter是否有一个API端点,它将过期的访问令牌与活动的访问令牌进行交换。我现在正在使用登录流程的方式是这样的。

// Request a token and redirect to the authorization page
$token = $this->twitter->getRequestToken();

// Set the session data
$this->session->set_userdata('oauth_token', $token['oauth_token']);
$this->session->set_userdata('oauth_token_secret', $token['oauth_token_secret']);

// Redirect the user to the authorization page
header('Location: https://api.twitter.com/oauth/authorize?oauth_token='.$token['oauth_token']);

用户重定向到的页面将提示用户在每次需要有效访问令牌时对我的应用进行授权。接受授权后,用户将被重定向到回调URL。在我的回调网址中,发生以下情况

// Get the parameters from the URL
$token = $this->input->get('oauth_token');
$verifier = $this->input->get('oauth_verifier');

$oauthToken = $this->session->oauth_token;
$oauthSecret = $this->session->oauth_token_secret;

// Get the access token
$access = $this->twitter->getAccessToken($verifier, $oauthToken, $oauthSecret);

是否存在生成访问令牌的方式而无需每次都授权我的应用程序?

1 个答案:

答案 0 :(得分:1)

根据Twitter's OAuth FAQ,除非用户明确拒绝您的申请或管理员暂停您的申请,否则令牌不会过期。

如果您希望您的用户能够重复登录而无需重新授权,则您需要提供一种存储令牌的机制(cookie,数据库等)。