镀铬延长件上的带电连接

Live connect on chrome extension

本文关键字:连接      更新时间:2023-09-26

你好,我正在尝试使用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扩展来做到这一点?

我终于成功了。只需遵循指南

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扩展中创建用于登录的UI,确保将重定向URL正确设置为"https://login.live.com/oauth20_desktop.srf并且响应类型设置为"token"
  4. 在Chrome扩展的内容脚本中,请注意Microsoft帐户登录流中的弹出窗口。在适当的时间点,我们将捕获auth_token,存储它,然后关闭弹出窗口

舱单应该是类似的东西

{
  "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();
        }
    }
});