javascript - Chrome identity launchWebAuthFlow 只打开空的回调页面

标签 javascript google-chrome-extension callback oauth-2.0 identity

对于另一个可能是新手的问题,我很抱歉,通常我不会放弃,直到我自己找到解决方案,但这个问题让我坚持了 3 天,是时候承认我被困住了......

我正在尝试验证 Chrome 扩展以通过 OAuth2 使用 PushBullet 用户数据:

background.js

var client_id = '<32 DIGIT CLIENT ID>'; 
var redirectUri = "chrome-extension://lgekckejcpodobwpelekldnhcbenimbe/oauth2";
var auth_url = "https://www.pushbullet.com/authorize?client_id=" + client_id + "&redirect_uri=" + encodeURIComponent(redirectUri) + "&response_type=token";

chrome.identity.launchWebAuthFlow({'url':auth_url,'interactive':true}, function(redirect_url){
    console.log(redirect_url)
});

list .json:

"permissions": [
    "identity", 
    "*://*.google.com/*",
    "*://*.pushbullet.com/*",   
    "storage"
  ],
  "web_accessible_resources": [ 
    "/oauth2/*"

当我加载扩展时:

  1. Pushbullet 授权弹出窗口打开并要求授予我的扩展权限(确定)
  2. 我同意(好)
  3. Pushbullet 窗口关闭并打开一个新的空白页面 该窗口是带有 token 的回调 URI:

chrome-extension://lgekckejcpodobwpelekldnhcbenimbe/oauth2#access_token=o.zrrWrDozxMu6kftrMHb89siYJQhRVcoL

我没想到会打开一个空页面,而是让 launchWebAuthFlow 捕获 URI 并将其写入控制台日志,就像在回调函数中编码一样...但它似乎在等待...

现在唯一的选择是关闭这个空白页面,只看到以下记录:

Unchecked runtime.lastError while running identity.launchWebAuthFlow: The user did not approve access.

显然我遗漏了一些重要的东西...我是否需要“某处”的额外代码来在我的 background.js 中获取回调 URI?

谢谢,非常感谢您的帮助。

暗影猎手

最佳答案

您误解了 identity API .

不能将它与自定义回调 URL 一起使用。 API 希望您使用以下形式的 URL

https://<app-id>.chromiumapp.org/*

您可以通过调用 chrome.identity.getRedirectURL(path) 获得

When the provider redirects to a URL matching the pattern https://<app-id>.chromiumapp.org/*, the window will close, and the final redirect URL will be passed to the callback function.

这是因为许多 OAuth 提供者不接受 chrome-extension:// URL 有效。

如果您这样做 - 很好,但您需要使用自己的 OAuth 库(和 token 存储,这更糟)。 chrome.identity仅适用于上述内容。

请注意网络请求实际上并没有发送到chromiumapp.org此流中的地址 - 它是 API 拦截的“虚拟”地址。

关于javascript - Chrome identity launchWebAuthFlow 只打开空的回调页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30612138/

相关文章:

google-chrome - 以编程方式访问所有新的 Chrome 通知

javascript - 在 onHeadersReceived 上修改标题

javascript - 我将如何构建一个 slider ,其中元素的不透明度降低,除了用户当前的那个?

首次运行后 Firefox 插件工具栏按钮消失

javascript - 使用 fetch 时如何选择退出 HTTP/2 服务器推送?

javascript - 尝试将数组从内容脚本发送到弹出脚本的 Chrome 扩展程序错误

javascript - 什么是闭包和回调?

javascript - 无法在使用 jquery html() 函数添加的 'new html code' 上执行 js 代码

javascript - 正则表达式 - 选择第一个空白字符之前的所有字符,假设它同时包含字符和数字

matlab - 在 MATLAB 中是否有触发任何其他回调的回调?