【问题标题】:Chrome.identity.getAuthToken not showing Google user listChrome.identity.getAuthToken 未显示 Google 用户列表
【发布时间】:2018-05-16 22:26:48
【问题描述】:

content.js这是chrome扩展的内容页面

document.getElementById("signIn").addEventListener("click", function(){
 chrome.runtime.sendMessage({task:"switchUser", user: current_user},function(response){                     
    });
});

background.js这是chrome扩展的背景页面

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse){
   if(request.task == "switchUser"){
    function getToken(){
       chrome.identity.getAuthToken({ interactive: true }, function(token) { 
        sendResponse(token);
      });
    }
      chrome.identity.removeCachedAuthToken({ token: 
       currentSessionAccessToken }, getToken);
    }
  return true;
});

先前的 OAuth 令牌已成功删除,但使用 getAuthToken 生成新令牌时,未显示用户选择列表。但是,我已将交互设置为 true。我错过了什么?

【问题讨论】:

    标签: google-chrome-extension oauth-2.0 identity


    【解决方案1】:

    您需要先撤销令牌,然后再从缓存中删除。请在下面找到 background.js 页面的代码。

    chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
    if(request.task == "switchUser"){
    function getToken(){
        chrome.identity.getAuthToken({ interactive: true }, function(token) { 
             sendResponse(token);
           });
         }
           var xmlHttp = new XMLHttpRequest();
           xmlHttp.open('GET', 'https://accounts.google.com/o/oauth2/revoke?token=' + currentSessionAccessToken); //revoke the token calling this url.
           xmlHttp.onload = function() { 
           chrome.identity.removeCachedAuthToken({ token: currentSessionAccessToken }, getToken); //after token is revoked, remove it from cache as well.
         }
           xmlHttp.onerror = function(){             
          };
          xmlHttp.send();
        }
       return true;
     });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多