[google chrome 28]
我chrome.experimental.identity在打包的应用程序中使用API 并且 getAuthToken 工作正常 - get 的令牌,我可以使用它获取用户信息等。我知道身份 API 正在从实验性转移到主干,例如从 chrome 29我将能够从我的清单中使用chrome.identity和删除"experimental"许可。
问:如果我想做一个注销按钮是removeCachedAuthToken怎么回事?我试图在experimental.identity 中使用它,但它什么也没做。
小智 7
要撤销令牌,请使用谷歌示例应用程序中的此函数。
function revokeToken() {
user_info_div.innerHTML = "";
chrome.identity.getAuthToken({ interactive: false },
function (current_token) {
if (!chrome.runtime.lastError) {
// @corecode_begin removeAndRevokeAuthToken
// @corecode_begin removeCachedAuthToken
// Remove the local cached token
chrome.identity.removeCachedAuthToken({token: current_token}, function(){});
// @corecode_end removeCachedAuthToken
// Make a request to revoke token in the server
var xhr = new XMLHttpRequest();
xhr.open(
"GET",
"https://accounts.google.com/o/oauth2/revoke?token=" + current_token);
xhr.send();
// @corecode_end removeAndRevokeAuthToken
// Update the user interface accordingly
changeState(STATE_START);
sampleSupport.log("Token revoked and removed from cache. " +
"Check chrome://identity-internals to confirm.");
}
});
}
Run Code Online (Sandbox Code Playgroud)
小智 5
不,这不是要走的路。
removeCachedAuthToken是一个函数,用于getAuthToken从内部令牌缓存中删除使用获取的令牌。但是,它不会撤销令牌。这意味着应用程序将无法再访问当前会话中的用户资源,直到它getAuthToken再次调用。发生这种情况时,它将能够再次获取令牌,而无需用户授予访问权限。
因此,此功能并不意味着是与注销相关的例程。它更像是一种恢复机制,当您意识到您的应用程序使用的访问令牌已过时或以任何其他方式无效时。当您使用访问令牌发出请求并且 HTTP 响应状态为 401 未授权时,就会发生这种情况。在这种情况下,您可以废弃令牌,然后使用 请求一个新令牌getAuthToken。要模拟这种行为,您可以使用Google 帐户页面或形成诊断 UI:chrome://identity-internals(目前它列出所有缓存的令牌)撤销相关授权。
请参阅GDocs 和 Identity的chrome 应用程序示例。(为 GDoc 拉取请求 114,为身份拉取请求 115,以防您在接下来的几天内这样做。)
| 归档时间: |
|
| 查看次数: |
1819 次 |
| 最近记录: |