21

我正在使用适用于 JavaScript 的 Google API 客户端库(Beta 版)在 Web 应用程序上授权用户 google 帐户(用于 youtube 操作)。一切正常,但我不知道如何从我的应用程序中“注销”用户,即重置访问令牌。

例如,以下代码检查用户授权,如果没有,则显示弹出窗口供用户登录帐户并允许 Web 应用程序访问用户数据:

gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: false}, handleAuth);

但是客户端库没有重置授权的方法。

有将用户重定向到“accounts.google.com/logout”的解决方法,但我不需要这种方法:因此我们不仅从我的应用程序,而且在任何地方都从谷歌帐户注销用户。

谷歌常见问题和客户端库描述都没有帮助。

4

5 回答 5

15

尝试撤销访问令牌,这应该会撤销实际授权,因此自动批准将停止工作。我认为这将解决您的问题。

https://developers.google.com/accounts/docs/OAuth2WebServer#tokenrevoke

于 2013-03-27T00:34:00.143 回答
8

它非常简单。只需撤销访问权限。

void RevokeAcess()
{
    try{
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("https://accounts.google.com/o/oauth2/revoke?token="+ACCESS_TOKEN);
    org.apache.http.HttpResponse response = client.execute(post);
    }
    catch(IOException e)
    {
    }
}

但它应该在 asyncTask

于 2014-04-23T13:29:14.850 回答
5

这取决于您重置授权的含义。我可以想到三种方法来做到这一点:

  1. 删除服务器上的授权
    转到 myaccount.google.com/permissions,找到您的应用并将其删除。下次您尝试登录时,您必须使用帐户选择器和同意屏幕完成完整的授权流程。

  2. 在客户端上
    注销 gapi.auth2.getAuthInstance().signOut();
    这样,Google 授权服务器仍会记住您的应用,并且授权令牌仍保留在浏览器存储中。

  3. 退出并断开
    gapi.auth2.getAuthInstance().signOut();
    gapi.auth2.getAuthInstance().disconnect();
    这等效于 (1) 但在客户端上。

于 2018-01-24T14:10:22.730 回答
0

只需使用:gapi.auth.setToken(null);

于 2013-11-03T15:59:30.853 回答
0

dotnet 的解决方案,调用下面的 API 并传递访问令牌,文档 - https://developers.google.com/identity/protocols/oauth2/web-server#tokenrevoke

        string url = "https://accounts.google.com/o/oauth2/revoke?token=" + profileToken.ProfileAccessToken;
        RestClient client = new RestClient(url);

        var req = new RestRequest(Method.POST);
        IRestResponse resp = client.Execute(req);
于 2021-03-10T14:21:53.070 回答