在chrome扩展上实时连接

时间:2015-03-26 17:41:21

标签: javascript google-chrome google-chrome-extension oauth-2.0 liveconnect

您好我正在尝试使用Microsoft OAuth,以便能够使用Chrome扩展程序内的Outlook凭据登录。

我正在使用javascript库(https://msdn.microsoft.com/en-us/library/hh550844.aspx),但我无法做到。我正在做以下事情。

WL.init({
    client_id: "foo_bar",
    scope: "wl.signin",
    redirect_uri:"http://www.contoso.com/redirect",
    response_type: "token" });

然后

WL.login()

正在发生的事情是我被重定向到http://www.contoso.com/redirect 但当我关闭弹出窗口时,我收到以下消息

  

[WL] WL.login:未经同意,弹出窗口已关闭。

我认为问题是redirect_uri但是如何使用chrome扩展程序?

1 个答案:

答案 0 :(得分:0)

我终于做到了。请遵循本指南

http://blogs.msdn.com/b/onenotedev/archive/2014/07/23/how-to-authenticate-with-microsoft-account-in-a-chrome-extension.aspx

你在这里有代码

https://github.com/jameslau-MSFT/MSAuthFromChromeExtSample

高级步骤

以下是您需要在高级别做的事情:

  1. 创建客户端ID并确保正确设置API设置。
  2. 正确设置Chrome扩展程序以使用至少1个内容脚本。我们将在下面的#4中使用它。
  3. 在Chrome扩展程序中创建用户界面以进行登录,确保将正确的重定向网址设置为“https://login.live.com/oauth20_desktop.srf”,并将响应类型设置为“令牌”。
  4. 在Chrome扩展程序的内容脚本中,从Microsoft帐户登录流程中查看弹出窗口。在正确的时间点,我们将捕获auth_token,存储它,然后关闭弹出窗口。
  5. 清单应该是这样的

    {
      "name": "MSAuthFromChromeExtSample",
        "short_name": "MSAChromeExt",
        "version": "1.0.0",
        "description": "Chrome extension that demonstrates how to authenticate against Microsoft Account.",
        /*"background":{
          "page": "background.html"
        },*/
        "browser_action": {
         /* "default_icon": {                   
            "19": "./assets/icons/icon-19.png",
            "38": "./assets/icons/icon-38.png"
          },*/
          "default_title": "MSA Auth Sample",
          "default_popup": "./html/popup.html"
        },
        "content_scripts": [
        {
          "matches": ["*://*/*"],
          "js": ["lib/jquery.min.js", "js/script.js"],
          "run_at" : "document_end"
        }
      ],
        "permissions": ["history","tabs","storage", "webRequest", "notifications", "<all_urls>"],
        "manifest_version": 2,
        "update_url": "http://clients2.google.com/service/update2/crx",
        "content_security_policy": "script-src 'self' https://js.live.net; object-src 'self'"
    }
    

    要指出的一些事项:

    • 我们将js / script.js作为内容脚本包含在内。每次在窗口或选项卡中加载文档时都会加载这些脚本。我们需要这个来执行上面的#4。我们还将lib / jquery.min.js作为内容脚本包含在内,因为我希望能够在我的script.js文件中使用jquery。
    • 我们在权限集中添加了“存储”,因为我们稍后会使用Chrome存储来存储auth_token。
    • 我们添加了这一行:“content_security_policy”:“script-src'self'https://js.live.net; object-src'self'”因此可以从popup.html成功加载LiveSDK JavaScript库
    • browser_action.default_popup设置为“./html/popup.html” - 这指定了当用户点击浏览器扩展程序按钮时显示的HTML。我们将使用它来显示登录UI。

    登录代码

    $('a#signin').click(function() {
        $('div#signin_status').text('');
        WL.init({
            client_id: "000000004410CD1A",    // replace with your own Client ID!!
            redirect_uri: "https://login.live.com/oauth20_desktop.srf",
            response_type: "token"
        });
        WL.login({
            scope: ["wl.signin", "office.onenote_create"]
        });
    
        return false;
    
    });
    

    内容脚本

    $(window).load(function() {
    
        if (window.location.origin == "https://login.live.com") {
    
            var hash = window.location.hash;
    
            // get access token
            var start = hash.indexOf("#access_token=");
            if ( start >= 0 ) {
                start = start + "#access_token=".length;
    
                var end = hash.indexOf("&token_type");
                var access_token = hash.substring(start, end);
    
                // Store it
                chrome.storage.local.set({"access_token":access_token}); 
    
                // Close the window
                window.close();
            }
        }
    });