【问题标题】:Chrome identity launchWebAuthFlow only opens empty callback pageChrome 身份 launchWebAuthFlow 只打开空的回调页面
【发布时间】:2023-03-11 08:13:01
【问题描述】:

很抱歉,又一个可能是菜鸟的问题,通常我不会放弃,直到我自己找到解决方案,但这个问题让我坚持了 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)
});

ma​​nifest.json:

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

当我加载扩展时:

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

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

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

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

运行 identity.launchWebAuthFlow 时未检查 runtime.lastError:用户未批准访问。

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

谢谢,真的很感谢你的帮助。

暗影猎手

【问题讨论】:

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


    【解决方案1】:

    你误会了identity API

    不能将它与自定义回调 URL 一起使用。 API 希望您使用表单的 URL

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

    您可以通过致电chrome.identity.getRedirectURL(path) 获得

    当提供者重定向到与https://&lt;app-id&gt;.chromiumapp.org/*模式匹配的URL时,窗口将关闭,最终的重定向URL将被传递给回调函数。

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

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

    请注意,网络请求实际上并未发送到此流程中的chromiumapp.org 地址——它是被 API 截获的“虚拟”地址。

    【讨论】:

    • 您好 Xan,非常感谢您...根据您的建议更改回调 URL 就可以了。现在完美运行,
    • 知道令牌应该存储在哪里吗? chrome 的本地存储安全吗?
    【解决方案2】:

    为其他可能会遇到困难的人快速阐述解决方案:

    这是工作代码:

    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)
        });
    

    ma​​nifest.js

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

    再次感谢 Xan,祝您有美好的一天。

    致以最诚挚的问候,

    暗影猎手

    【讨论】:

    • 真的想在此处公开该客户端 ID 吗?如果不是,请编辑以将 ID 替换为明显伪造的内容,然后重新生成 ID。您还可以标记您的答案以引起版主注意,要求擦除编辑历史记录。
    • 嗨,Xan,别担心,这是假的……但你是对的,我对其进行了编辑以使其更清晰……感谢您指出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多