本文介绍了Chrome 身份启动WebAuthFlow 只打开空的回调页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉又一个可能是菜鸟的问题,通常我不会屈服,直到我自己找到解决方案,但这个问题让我坚持了 3 天,是时候承认我被卡住了......

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

background.js

var 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){控制台日志(redirect_url)});

ma​​nifest.json:

权限":[身份","*://*.google.com/*","*://*.pushbullet.com/*",贮存"],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?

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

暗影猎手

解决方案

你误解了 身份 API.

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

https://.chromiumapp.org/*

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

获得

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

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

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

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

Sorry for yet another probably noob question, normally I don't give in until I find a solution myself but this one has me going for 3 days and it is time to admit I'm stuck...

I'm trying to authenicate a Chrome extension to use PushBullet user data via OAuth2:

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

manifest.json:

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

When I load the extension:

  1. The Pushbullet authorization pop-up opens and asks to give permission to my extension (OK)
  2. I agree (OK)
  3. The Pushbullet window closes and a new empty page opes the URL ofthat windows is the callback URI with a token:

I did not expect an empty page to open but rather having launchWebAuthFlow captured the URI and have it written in the console log like coded in the callback function... but it seems to be waiting...

The only option now is to close this empty page only to see the following logged:

Clearly I'm missing something vital... do I need additional code "somewhere" to get the callback URI in my background.js?

Thanks, really appriciate the help.

ShadowHunter

解决方案

You are misunderstanding the identity API.

You cannot use it with a custom callback URL. The API expects you to use a URL of the form

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

which you can obtain with a call to chrome.identity.getRedirectURL(path)

This is because a lot of OAuth providers would not accept a chrome-extension:// URL as valid.

If your does - great, but you'll need to use your own OAuth library (and token storage, which is worse). chrome.identity works only with the above.

Do note that the network request is not actually sent to the chromiumapp.org address in this flow - it's a "virtual" address intercepted by the API.

这篇关于Chrome 身份启动WebAuthFlow 只打开空的回调页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:36