2017-12-03 274 views
0

content.js这是Chrome扩展页面内容Chrome.identity.getAuthToken没有显示谷歌的用户列表

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

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); 
     }); 
    } 
     chrome.identity.removeCachedAuthToken({ token: 
     currentSessionAccessToken }, getToken); 
    } 
    return true; 
}); 

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

回答

0

您需要首先撤销令牌,然后从缓存中删除。请在下面找到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; 
});