使用 Twitter 刷新令牌 API

Refresh token with Twitter API

我想知道 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。在我的回调 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);

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

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

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