对于另一个可能是新手的问题,我很抱歉,通常我不会放弃,直到我自己找到解决方案,但这个问题让我坚持了 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/*"
当我加载扩展时:
- Pushbullet 授权弹出窗口打开并要求授予我的扩展权限(确定)
- 我同意(好)
- 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/