2012-04-24 14 views
15

SignIn butonuna tıklamak zorunda kalmadan bir Uygulama için Canlı (Silverlight, WP7 can) giriş yapmanın bir yolu var mı?Live SDK - SignInButton olmadan oturum açmaya çalışın

Dinamik olarak oturum açmak istiyorum, örneğin: uygulamayı başlattığınızda, benimle oturum açmak istiyorum. Düğmeye başvurmadan nasıl yapılır?

cevap

29

ben nasıl anladım, bu yüzden paylaşmaya karar: Kod örneğin

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

kredi bekliyor: http://social.msdn.microsoft.com/Forums/en-GB/messengerconnect/thread/ a1ae8e9c-47a5-4bd8-b821-33dc1a0d6e94 – Richard

+2

Teşekkürler. Microsoft bunu belgelemelidir. – ashraf

+0

Bu kısa ve tatlı kod parçası için çok teşekkürler. LiveAuthClient'i nasıl kullanacağım konusunda net bir fikre kapılmıyordum. Live SDK için yeni MSDN belgeleri çok yanlış, [LiveAuthClient (String) yöntemi] [1] dize parametresinin yönlendirme URI'sı olduğunu söylüyor. ClientID hakkında bir şey söylemez. [1] http://msdn.microsoft.com/en-us/library/live/microsoft.live.liveauthclient.aspx – Adarsha

1

Teşekkür - Beni Windows Phone 8, vb :)

için kod güncelleme sürümü ile gelip yardımcı
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 
     } 
    } 
} 
+0

Aşağıdaki kod veren aşağıdaki hata-'Microsoft.Live.LiveAuthException' türünde bir istisna mscorlib.ni.dll dosyasında oluştu, ancak kullanıcı kodunda işlenmedi –

10

Güzel cevap Richard. Bu gerçekten çok yardımcı oldu.

InitializedCompleted olayını bulamadıklarını belirten kişilerden birkaç yorum aldım. .Net 4.5 kodluyorsanız, zaman uyumsuz yöntemler için async/await modelini izlemeniz gerekir. Yukarıdaki sınıf şu şekilde görünecektir:

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 bir zaman uyumsuz olması astar here

İlgili konular