Live SDK - Try to Sign In without SignInButton

大兔子大兔子 提交于 2019-12-03 02:01:55

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

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

Thanks for the code example - helped me come up with the update version of code for Windows Phone 8, etc. :)


using System.Windows;
using Microsoft.Live;

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

    private LiveConnectClient _connection;
    private LiveLoginResult _login;

    public LiveLogin()
    {
        this.Loaded += this.OnLoaded;
    }

    private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
    {
        //----------------------------------------------------------------------
        // Login to skydrive
        //----------------------------------------------------------------------
        await SkydriveLogin();
    }

    private async Task SkydriveLogin()
    {
        try
        {
            //----------------------------------------------------------------------
            // Initialize our auth client with the client Id for our specific application
            //----------------------------------------------------------------------
            LiveAuthClient authClient = new LiveAuthClient("**your client id here**");

            //----------------------------------------------------------------------
            // Using InitializeAsync we can check to see if we already have an connected session
            //----------------------------------------------------------------------
            _login = await authClient.InitializeAsync(_scopes);

            //----------------------------------------------------------------------
            // If not connected, bring up the login screen on the device
            //----------------------------------------------------------------------
            if (_login.Status != LiveConnectSessionStatus.Connected)
            {
                _login = await authClient.LoginAsync(_scopes);
            }

            //----------------------------------------------------------------------
            // Initialize our connection client with our login result
            //----------------------------------------------------------------------
            _connection = new LiveConnectClient(_login.Session);
        }
        catch (Exception ex)
        {
            //TODO: Add connection specific exception handling
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!