如何撤销 Twitter 通过 Twitter REST 授予应用程序的用户访问令牌 API

How to revoke user access token granted by Twitter to an app via Twitter REST API

我有一个 Node JS 应用程序,它使用 Twitter API(使用 OAuth 1.0a)对用户进行身份验证,并使用提供的用户访问令牌代表用户 posts 推文。

我还想通过撤销之前提供的访问令牌,为我的应用程序的用户提供一种从 Twitter "disconnect" 的方式,这样用户下次想要访问时必须登录 Twitter通过我的应用程序。

我也与 Facebook 进行了类似的集成(通过 OAuth 2),使用 Facebook 的合法方式是通过对 /me/permissions 路由的 DELETE 请求。对于 Twitter,我找到了一种方法(在此页面上提到:https://dev.twitter.com/oauth/reference/post/oauth2/invalidate/token)使访问令牌无效,但它似乎失败并出现 "Unable to verify your credentials" 错误。我知道我提供了正确的凭据,因为如果我只是 post 一条具有相同凭据的推文,而不是 posting 到那个端点,我能够成功地做到这一点。

需要注意的一点:我发现Twitter不支持OAuth2进行用户认证,这就是我为Twitter使用OAuth 1的原因。不知道跟这个有没有关系。

我的应用中试图撤销令牌的示例代码:

var logout = function(userAccessToken, userAccessTokenSecret, done) {
    oauth.post(
      "https://api.twitter.com/oauth2/invalidate_token",
      userAccessToken, userAccessTokenSecret,
      {"access_token": userAccessToken},
      "application/json",
      function(error, data) {
        console.log("response from Twitter: error = " + JSON.stringify(error) + ", data = " + JSON.stringify(data));
        done(error, data);
      }
    );
}

目前看来这是不可能的。您提供的路线仅适用于您可以通过

获得的仅限应用程序的令牌

https://dev.twitter.com/oauth/reference/post/oauth2/token

根据常见问题解答部分,用户可以在其设置中自行撤销令牌。

此外,我在 Twitter 社区中发现了这个 post,它问的问题与您基本相同:

https://twittercommunity.com/t/actively-revoking-oauth-access-tokens/1505

There's currently no way for an application to voluntarily revoke the access tokens for a user who has granted an application access. If you determine a certain period of inactivity on your service as implying the user is no longer using your application, your best avenue is to remove the access token from your records. If the user ever comes back, you can re-negotiate it for them.

所以在我看来最简单的方法就是删除你这边的令牌。