【问题标题】:How do I log out of a chrome.identity oauth provider如何注销 chrome.identity oauth 提供程序
【发布时间】:2014-11-22 17:00:31
【问题描述】:

我正在使用 chrome.identity 在 chrome 扩展程序中登录到第 3 方 oauth 提供程序。它可以很好地登录 - 当我使用 launchWebAuthFlow 时,我会看到第三方登录屏幕,并在登录流程后重定向回我的应用程序。

但是,我找不到在我的扩展程序中启用注销功能的方法。似乎没有清除缓存的登录身份的功能。下次调用launchWebAuthFlow时,它会自动让我作为第一个用户登录,不会提示我再次登录。

有没有办法清除chrome.identity插件的登录状态?

【问题讨论】:

标签: google-chrome-extension oauth


【解决方案1】:

如果您尝试 launchWebAuthFlow 注销但得到 User interaction required 错误,那么您需要在 url 中再添加一个标志:

chrome.identity.launchWebAuthFlow (
  {'url': 'https://some-logout-url/',
   'interactive': true },
   function(result) {
     console.log(result);
   }
);

【讨论】:

  • 没错。大多数用户会更喜欢 ERROR。如果您喜欢错误而不是弹出窗口,只需将代码保留在接受的答案中即可:-)
  • 弹出窗口要求人们登录。当用户单击注销时,这不是您想要的。最终,我认为要完成这项工作需要做更多的工作。
【解决方案2】:

只有通过这个实现我才能取得成果

  chrome.identity.getAuthToken({ 'interactive': false }, currentToken => {
    if (!chrome.runtime.lastError) {
      // Remove the local cached token
      chrome.identity.removeCachedAuthToken({ token: currentToken }, () => {})

      // Make a request to revoke token in the server
      const xhr = new XMLHttpRequest()
      xhr.open('GET', `${googleRevokeApi}${currentToken}`)
      xhr.send()

      // Update the user interface accordingly
      // TODO: your callback
    }
  })

【讨论】:

    【解决方案3】:

    我发现按顺序调用这两个是有效的:

    var url = 'https://accounts.google.com/o/oauth2/revoke?token=' + token;
    window.fetch(url);
    
    chrome.identity.removeCachedAuthToken({token: token}, function (){
      alert('removed');
    });
    

    【讨论】:

    • 注意这将全局注销用户;来自所有页面和 Chrome 浏览器。可能不是您想要的扩展程序。
    【解决方案4】:

    对我来说,https://accounts.google.com/logout 不起作用。但是https://accounts.google.com/o/oauth2/revoke?token=TOKEN 工作得很好,使用简单的window.fetch(url),而不是hrome.identity.launchWebAuthFlow

    【讨论】:

      【解决方案5】:

      您应该将 prompt=select_account 添加到您的身份验证 URL。您的问题将得到解决。

      https://accounts.google.com/o/oauth2/auth?client_id={clientId}&response_type=token&scope={scopes}&redirect_uri={redirectURL}&prompt=select_account

      【讨论】:

        【解决方案6】:

        我最近碰巧遇到了同样的问题,我终于通过在登录网址中添加login_hint=<new_user>prompt=consent解决了。

        【讨论】:

          【解决方案7】:

          我不知道具体的第三方提供商。但是在将 Google Oauth 与 chrome.identity.launchWebAuthFlow() 一起使用时,我遇到了类似的问题。我可以登录用户,但不能使用 removeCachedAuthToken() 退出

          在这种情况下,为了注销用户,我使用 chrome.identity.launchWebAuthFlow() 和 Google 的注销 URL,而不是 oauth URL

          chrome.identity.launchWebAuthFlow(
              { 'url': 'https://accounts.google.com/logout' },
              function(tokenUrl) {
                  responseCallback();
              }
          );
          

          效果很好。

          【讨论】:

          • 谢谢。经过数周的搜索,这对我来说是关于 WordPress OAuth2 的诀窍。
          • 我如何让它在 Facebook 注销时起作用?这对我不起作用:/
          • 我使用这种方法时出错,chrome.runtime.lastError = "User interaction required"
          • 交互式注销需要额外的标志。详情请看我的回答。
          • 这个序列对我有用:stackoverflow.com/a/50343218/851957
          【解决方案8】:

          您可以使用chrome.identity.removeCachedAuthToken(object details, function callback) 方法清除身份缓存。
          https://developer.chrome.com/apps/identity#method-removeCachedAuthToken

          【讨论】:

          • 我应该在问题中提到我已经尝试过这种方法,但它不起作用。我查看了 chrome 扩展支持论坛和其他使用 launchWebAuthFlow 的用户报告相同的problem。 removeCachedAuthToken 方法似乎只在使用 google 作为 oauth 提供者时才有效;它似乎不适用于第三方。
          猜你喜欢
          • 2018-09-11
          • 2020-11-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-11-21
          • 1970-01-01
          • 2011-08-30
          • 1970-01-01
          相关资源
          最近更新 更多