windowsapioauth-2.0windows-live

How to retrieve oAuth Accees token from Redirect Window


I am working on an app where i am communicating to Windows API. I am using oAuth 2.0 for the same.

Complete code has been done using JS/HTML5 only. However i am facing one issue,

whenever i request for access token, it opens a new windows with my redirect url appended with access token and other parameters. But the token is not sent back to my code. I have to manually copy the code and thus it defeats purpose of my application. Is there any way , when i click on button (that invokes my oAuth call), a new pop up window appears and redirects back to my called url with access token ?

here is what i have done so far:

    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;

And this is where i am stuck. Can anyone pls help. Also greetUser function is not working. I want this to work as client side only using js/html only. `


Solution

  • Not sure what you are trying to do with the tokenAuth. Got this working which should help you with the greeting part:

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