删除扩展程序的权限

Removing permissions of the extensions

我有一个扩展程序首先请求访问 Google 云端硬盘文件的权限。扩展几乎是空的,除了在我加载这个 js 的弹出窗口中:

chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
  // Use the token.
  console.log('Request Token')
  console.log(token)
  chrome.identity.removeCachedAuthToken(
              { 'token': token }, function () {})
  console.log('Removed token')
});

在我的清单中,我有有效的密钥、oauth2 客户端 ID 和 "scopes":["https://www.googleapis.com/auth/drive"] 除了 chrome 扩展的其他标准键。

它工作正常,它首先请求许可然后记录我的访问令牌。但是,当我重新安装扩展程序 (deleted/modified/added) 时,它没有征求我的许可,只是写了相同的访问令牌。我想再次请求许可。我怎样才能做到这一点?

一旦您授予权限,当然不会再提示您。您需要进入 Google 帐户页面并撤销权限。

为了删除权限,我必须添加另一个 GET 请求来撤销权限:

chrome.identity.getAuthToken({ 'interactive': true }, function(token) {
  // Use the token.
  if (token) {
        // Make a request to revoke token
        var xhr = new XMLHttpRequest();
        xhr.open('GET', 'https://accounts.google.com/o/oauth2/revoke?token=' +
             token);
        xhr.send();
   }
  chrome.identity.removeCachedAuthToken(
              { 'token': token }, function () {})
});

这就是诀窍,现在每次我打开弹出窗口时都会提示权限。

但还有另一个问题:当我授予权限时,我得到

XMLHttpRequest cannot load https://accounts.google.com/o/oauth2/revoke?token=... 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'chrome-extension://acfnd...' is therefore not allowed access.

我不确定是什么意思。

在开发过程中,您可以前往 chrome://identity-internals 撤销特定令牌。下次您授权该用户时,权限对话框将再次显示。记录在 User Authentication: Caching.