如何从重定向窗口检索oAuth访问令牌

时间:2012-08-10 03:49:37

标签: windows api oauth-2.0 windows-live

我正在开发一个应用程序,我正在与Windows API进行通信。我正在使用oAuth 2.0。

完整代码仅使用JS / HTML5完成。但是我面临一个问题,

每当我请求访问令牌时,它都会打开一个新窗口,其中我的重定向URL附加了访问令牌和其他参数。但令牌不会发送回我的代码。我必须手动复制代码,因此它违背了我的应用程序的目的。有什么办法,当我点击按钮(调用我的oAuth调用)时,会出现一个新的弹出窗口,并重定向回带有访问令牌的被叫url?

这是我到目前为止所做的:

    var APPLICATION_CLIENT_ID = 'SOME_NUMBERS',
            REDIRECT_URL = "http://www.myweb.com";

    WL.Event.subscribe("auth.login", onLogin);
    WL.init({
        client_id: APPLICATION_CLIENT_ID,
        redirect_uri: REDIRECT_URL,
        scope: 'wl.skydrive_update',
        response_type: "token"
    });
    WL.ui({
        name: "signin",
        element: "signInButton",
        brand: "hotmail",
        type: "connect"
    });
    function greetUser(session) {
        var strGreeting = "";
        WL.api(
                {
                    path: "me",
                    method: "GET"
                },
                function (response) {
                    if (!response.error) {
                        strGreeting = "Hi, " + response.first_name + "!"
                        document.getElementById("greeting").innerHTML = strGreeting;
                    }
                });
    }

    function onLogin() {
        var session = WL.getSession();
        if (session) {
            greetUser(session);
        }
    }

    var tokenAuth = //Adding Manually// 

    var apiURL = "https://apis.live.net/v5.0/me/";
    var tokenAuthParam = "?access_token=" + tokenAuth;

这就是我被困住的地方。任何人都可以帮助。此外greetUser功能不起作用。我希望这只作为客户端工作,仅使用js / html。 `

1 个答案:

答案 0 :(得分:0)

不确定您尝试使用tokenAuth做什么。得到这个工作,它可以帮助你解决问候部分:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>JScript Win 8 example</title>

    <!-- WinJS references -->
    <link href="//Microsoft.WinJS.1.0.RC/css/ui-dark.css" rel="stylesheet" />
    <script src="//Microsoft.WinJS.1.0.RC/js/base.js"></script>
    <script src="//Microsoft.WinJS.1.0.RC/js/ui.js"></script>

    <script type="text/javascript" src="/LiveSDKHTML/js/wl.js"></script>

    <link href="/css/default.css" rel="stylesheet" />
    <script src="/js/default.js"></script>
</head>
<body>
    <div id="signin"></div>
    <br />
    <label id="infoLabel"></label>
    <br />
    id: <label id="id"></label><br />
    firstname: <label id="first_name"></label><br />
    lastname: <label id="last_name"></label><br />
    fullname: <label id="name"></label><br />
    gender: <label id="gender"></label><br />
    locale: <label id="locale"></label><br />
    <script>
        WL.Event.subscribe("auth.login", onLogin);
        WL.init({
            scope: "wl.signin",            
        });
        WL.ui({
            name: "signin",
            element: "signin"
        });

        function onLogin(session) {
            var session = WL.getSession();
            if (session.error) {
                document.getElementById("infoLabel").innerText = "Error signing in: " + session.error;
            }
            else {
                document.getElementById("infoLabel").innerText = "Signed in.";

                WL.api(
                    "/me", "GET",
                    function (response) {
                        if (!response.error) {
                        document.getElementById("id").innerText = response.id;
                        document.getElementById("first_name").innerText = response.first_name;
                        document.getElementById("last_name").innerText = response.last_name;
                        document.getElementById("name").innerText = response.name;
                        document.getElementById("gender").innerText = response.gender;
                        document.getElementById("locale").innerText = response.locale
                        }
                        else {
                        document.getElementById("infoLabel").innerText = "API call failed: " + JSON.stringify(response.error).replace(/,/g, "\n");
                        }
                    }
                );
            }
        }

    </script>  

</body>
</html>