Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Live SDK - Try to Sign In without SignInButton

Is there any way to login to Live for an App (Silverlight, WP7 can) without having to click on SignIn button.

I want to log me dynamically, for example: when you start the app, I want to log in to me. How to do this without resorting to the button?

like image 582
richardaum Avatar asked Apr 24 '12 18:04

richardaum


2 Answers

I figured out how to do, so I decided to share:

using System.Windows;
using Microsoft.Live;

public class LiveLogin
{

    private static readonly string[] scopes = 
        new string[] { 
            "wl.signin", 
            "wl.basic", 
            "wl.calendars", 
            "wl.calendars_update", 
            "wl.contacts_calendars", 
            "wl.events_create" };

    private LiveAuthClient authClient;
    private LiveConnectClient liveClient;


    public LiveLogin()
    {
        this.authClient = new LiveAuthClient("**your client id here**");
        this.authClient.InitializeCompleted += authClient_InitializeCompleted;
        this.authClient.InitializeAsync(scopes);
    }

    private void authClient_InitializeCompleted(object sender, LoginCompletedEventArgs e)
    {
        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            this.liveClient = new LiveConnectClient(e.Session);
        }
        else
        {
            this.authClient.LoginCompleted += authClient_LoginCompleted;
            this.authClient.LoginAsync(scopes);
        }
    }

    private void authClient_LoginCompleted(object sender, LoginCompletedEventArgs e)
    {
        if (e.Status == LiveConnectSessionStatus.Connected)
        {
            this.liveClient = new LiveConnectClient(e.Session);
            MessageBox.Show("Signed");
        }
        else
        {
            MessageBox.Show("Failed!");
        }
    }
}
like image 73
richardaum Avatar answered Oct 31 '22 18:10

richardaum


Great answer Richard. This really helped a lot.

I noticed a couple of comments from people complaining that they couldn't find the InitializedCompleted event. If you're coding in .Net 4.5 then you need to follow the async/await pattern for asynchronous methods. The class above would look like this:

public class LiveLogin
    {
        private static readonly string[] Scopes =
            new[]
                {
                    "wl.signin",
                    "wl.basic",
                    "wl.calendars",
                    "wl.calendars_update",
                    "wl.contacts_calendars",
                    "wl.events_create"
                };

        private LiveAuthClient _authClient;



        public async Task<LiveConnectClient> Login()
        {
            _authClient = new LiveAuthClient("**your client id here**");

            LiveLoginResult result = await _authClient.InitializeAsync(Scopes);
            if (result.Status == LiveConnectSessionStatus.Connected)
            {
                return new LiveConnectClient(result.Session);
            }
            result = await _authClient.LoginAsync(Scopes);
            if (result.Status == LiveConnectSessionStatus.Connected)
            {
                return new LiveConnectClient(result.Session);
            }
            return null;
        }


    }

MS have an async await primer here

like image 10
Faster Solutions Avatar answered Oct 31 '22 16:10

Faster Solutions