2

我使用chrome.identity.launchWebAuthFlow(),当他们使用我的分机直接通过非谷歌的API的OAuth2用户。打开chrome.identity.launchWebAuthFlow作为接头(Chrome扩展)

即使验证工作,我可以有效地获取用户的身份验证令牌,我不喜欢它在缺乏浏览器的用户界面和URL栏的新窗口完全打开。

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

眼下,用户会点击扩展开辟弹出。在弹出窗口中,有一个按钮,他们将点击以激活OAuth。从这里,弹出一个完整的窗口,但我希望它保持在弹出窗口内(参见上文)。

我怎样才能做到这一点?我已经看到很多关于它的讨论,但没有解决方案。

谢谢!

的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> 

回答

0

您将无法获得的OAuth流在弹出的工作,但有控制与浏览器标签流自己的full walkthrough

+0

这是演练不幸的是,您好!OAuth1和过时的。 – user2494584

+0

选项卡的处理将是相同的。只需将OAuth 1代码替换为OAuth 2代码即可。 – abraham