打开chrome.identity.launchWebAuthFlow作为选项卡(Chrome扩展程序) [英] Open chrome.identity.launchWebAuthFlow as a tab (Chrome Extensions)

查看:254
本文介绍了打开chrome.identity.launchWebAuthFlow作为选项卡(Chrome扩展程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 chrome.identity.launchWebAuthFlow()来指导用户在使用我的扩展程序时通过非Google API的OAuth2。



即使身份验证正常,我也可以有效地获取用户的身份验证令牌,但我不喜欢它会在一个缺少浏览器UI和URL栏的新窗口中完全打开。



理想情况下,我可以在扩展程序的弹出窗口中执行此OAuth舞蹈。如果这不起作用,至少能够在单独的选项卡中进行此OAuth事件是另一种解决方案。



现在,用户将点击扩展名打开弹出窗口。在弹出窗口中,有一个按钮,他们将点击以激活OAuth。从这里开始,另一个窗口弹出,但我希望它保留在弹出窗口中(见上面)。



我该如何做到这一点?我已经看到很多关于它的讨论,但没有解决方案。

strong> manifest.json

  {
manifest_version:2,
name :Extension,
version:0.0.1,
permissions:[
identity,
tabs,
http:// * / *,
https:// * / *

browser_action:{
default_icon:icon.png ,
default_popup:popup.html
},

background:{
scripts:[background.js]


background.js

  chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if(request。消息===start_oauth){
chrome.identity.launchWebAuthFlow(
{
url:https://example.com/manage/oauth2/authorize?callba ck_uri =
+ encodeURIComponent(< callback_uri>)
+& client_id =+< client_id>
+& response_type = code,
interactive:true
},
function(redirect_url){
var auth_token = redirect_url.substr(< callback_uri>。长度+ 6);
}
);
}
}
);

popup.js

 函数startOAuth(){
chrome.runtime.sendMessage({message:start_oauth});
}
document.getElementById(login)。addEventListener(click,startOAuth);

popup.html

 < html> 
< body>
< button id =login>点击此处登录< /按钮>
< / body>
< script src =popup.js>< / script>
< / html>


解决方案

您将无法获得OAuth流在弹出窗口中工作,但有一个完整演练,可通过浏览器选项卡自行控制流程。

I am using chrome.identity.launchWebAuthFlow() to direct a user through OAuth2 of a non-Google API when they use my extension.

Even though authentication works and I can effectively get the user's authentication token, I dislike that it opens up entirely in a new window that lacks the browser's UIs and a URL bar.

Ideally, I would be able to conduct this OAuth dance within the extension's popup window. If that doesn't work, being able to at least have this OAuth event take place in a separate tab is another solution.

Right now, the user will click on the extension to open up the popup. On the popup, there is a button that they will click to activate OAuth. From here, a whole other window pops up, but instead I'd like it to stay within the popup (see above).

How can I make this happen? I've seen a lot of talk about it, but no solution for it.

Thanks!

manifest.json

{
    "manifest_version": 2,
    "name": "Extension",
    "version": "0.0.1",
    "permissions": [
        "identity",
        "tabs",
        "http://*/*", 
        "https://*/*"
    ],
    "browser_action":{
        "default_icon": "icon.png",
        "default_popup" : "popup.html"
    },

    "background": {
        "scripts": ["background.js"]
    }
}

background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse){
        if( request.message==="start_oauth"){
            chrome.identity.launchWebAuthFlow(
                {
                    "url" : "https://example.com/manage/oauth2/authorize?callback_uri="
                    +encodeURIComponent(<callback_uri>)
                    +"&client_id=" + <client_id>
                    +"&response_type=code",
                "interactive" : true
                },
                function(redirect_url){
                    var auth_token = redirect_url.substr(<callback_uri>.length+6);
                }
            );
        }
    }
);

popup.js

function startOAuth(){
    chrome.runtime.sendMessage({"message": "start_oauth"});
}
document.getElementById("login").addEventListener("click", startOAuth);

popup.html

<html>
    <body>
        <button id="login"> Click here to log in</button>
    </body>
    <script src="popup.js"></script>
</html>

解决方案

You won't be able to get the OAuth flow working in a popup but there is a full walkthrough of controlling the flow yourself with browser tabs.

这篇关于打开chrome.identity.launchWebAuthFlow作为选项卡(Chrome扩展程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆