Lance Larsen - .NET Architect Madison Wisconsin

Persisting SkyDrive Login on Windows Phone 8 via LiveSDK

Been having a lot of fun with working on Windows Phone 8 apps – specifically using the LiveSDK to integrate to use SkyDrive.  Good examples with authenticating and getting the OAuth login window to pop-up for the user – but unless you want that to happen every time the user bring us your application – you need to take advantage of the InitalizeAsync() method in the fashion below.  Didn’t find many ( if any ) great examples of doing this – so when I pieced it all together – wanted to share with other WP8 developers. :)  Good Coding! Enjoy…

   1:  using System.Windows;
   2:  using Microsoft.Live;
   3:   
   4:  public class LiveLogin : PhoneApplicationPage
   5:  {
   6:      private static readonly string[] _scopes =
   7:          new[] { 
   8:          "wl.signin", 
   9:          "wl.basic" };
  10:   
  11:      private LiveConnectClient _connection;
  12:      private LiveLoginResult _login;
  13:   
  14:      public LiveLogin()
  15:      {
  16:          this.Loaded += this.OnLoaded;
  17:      }
  18:   
  19:      private async void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
  20:      {
  21:          //----------------------------------------------------------------------
  22:          // Login to skydrive
  23:          //----------------------------------------------------------------------
  24:          await SkydriveLogin();
  25:      }
  26:   
  27:      private async Task SkydriveLogin()
  28:      {
  29:          try
  30:          {
  31:              //----------------------------------------------------------------------
  32:              // Initialize our auth client with the client Id for our specific application
  33:              //----------------------------------------------------------------------
  34:              LiveAuthClient authClient = new LiveAuthClient("**your client id here**");
  35:   
  36:              //----------------------------------------------------------------------
  37:              // Using InitializeAsync we can check to see if we already have an connected session
  38:              //----------------------------------------------------------------------
  39:              _login = await authClient.InitializeAsync(_scopes);
  40:   
  41:              //----------------------------------------------------------------------
  42:              // If not connected, bring up the login screen on the device
  43:              //----------------------------------------------------------------------
  44:              if (_login.Status != LiveConnectSessionStatus.Connected)
  45:              {
  46:                  _login = await authClient.LoginAsync(_scopes);
  47:              }
  48:   
  49:              //----------------------------------------------------------------------
  50:              // Initialize our connection client with our login result
  51:              //----------------------------------------------------------------------
  52:              _connection = new LiveConnectClient(_login.Session);
  53:          }
  54:          catch (Exception ex)
  55:          {
  56:              //TODO: Add connection specific exception handling
  57:          }
  58:      }
  59:  }
 
Comments

Trackbacks for this post

Leave a Reply