Chrome身份启动WebAuthFlow仅打开空回调页面

Sha*_*ter 12 javascript identity callback google-chrome-extension oauth-2.0

对不起另一个可能是noob的问题,通常我不会放弃,直到我自己找到一个解决方案,但这个让我去了3天,现在是时候承认我被卡住了...

我正在尝试通过OAuth2验证Chrome扩展程序以使用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)
});
Run Code Online (Sandbox Code Playgroud)

manifest.json的:

"permissions": [
    "identity", 
    "*://*.google.com/*",
    "*://*.pushbullet.com/*",   
    "storage"
  ],
  "web_accessible_resources": [ 
    "/oauth2/*"
Run Code Online (Sandbox Code Playgroud)

当我加载扩展时:

  1. Pushbullet授权弹出窗口打开并要求授予我的分机(OK)
  2. 我同意(好的)
  3. Pushbullet窗口关闭,一个新的空页面打开该窗口的URL是带有令牌的回调URI:

镀铬的扩展://lgekckejcpodobwpelekldnhcbenimbe/oauth2#access_token=o.zrrWrDozxMu6kftrMHb89siYJQhRVcoL

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

现在唯一的选择是关闭此空白页面以查看以下记录:

运行identity.launchWebAuthFlow时未经检查的runtime.lastError:用户未批准访问权限.

很明显我错过了一些重要的东西...我需要在"某个地方"使用额外的代码来获取我的background.js中的回调URI吗?

谢谢,真的很高兴帮助.

暗影猎手

Xan*_*Xan 16

您误解了identityAPI.

无法将其与自定义回调网址一起使用.API希望您使用表单的URL

https://<app-id>.chromiumapp.org/*
Run Code Online (Sandbox Code Playgroud)

你可以通过电话获得 chrome.identity.getRedirectURL(path)

当提供程序重定向到与模式匹配的URL时https://<app-id>.chromiumapp.org/*,窗口将关闭,最终的重定向URL将传递给回调函数.

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

如果您的确如此 - 但是您需要使用自己的OAuth库(以及令牌存储,这更糟).chrome.identity仅适用于上述情况.

请注意,网络请求实际上并未发送chromiumapp.org到此流程中的地址 - 它是API拦截的"虚拟"地址.


Sha*_*ter 9

对可能与之抗争的其他任何人的解决方案进行快速阐述:

这是工作代码:

background.js

var client_id = '<CLIENT_ID>';
var redirectUri = chrome.identity.getRedirectURL("oauth2");     
var auth_url = "https://www.pushbullet.com/authorize?client_id=" + client_id + "&redirect_uri=" + redirectUri + "&response_type=token";

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

manifest.js

"permissions": [
    "identity", 
    "*://*.google.com/*",
    "*://*.pushbullet.com/*",   
    "storage"
  ],      
Run Code Online (Sandbox Code Playgroud)

再次,谢谢Xan,祝你有个美好的一天.

最诚挚的问候,

暗影猎手