2017-12-03 241 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; 
});