2012-04-24 50 views
15

有沒有任何方法可以登錄到Live應用程序(Silverlight,WP7可以),而無需點擊登錄按鈕。實時SDK - 嘗試登錄而不登錄按鈕

我想動態登錄我,例如:當您啓動應用程序時,我想登錄到我。如何做到這一點,而不訴諸按鈕?

回答

29

我想通了,怎麼辦,所以我決定分享到:

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

學分:http://social.msdn.microsoft.com/Forums/en-GB/messengerconnect/thread/ a1ae8e9c-47a5-4bd8-b821-33dc1a0d6e94 – Richard 2012-04-25 21:42:05

+2

謝謝。微軟應該記錄它。 – ashraf 2012-05-11 16:28:39

+0

非常感謝這段短而甜美的代碼。我沒有清楚如何使用LiveAuthClient。 Live SDK的新MSDN文檔非常錯誤,[LiveAuthClient(String)method] [1]表示字符串參數是重定向URI。它沒有提到有關ClientID的任何信息。 [1] http://msdn.microsoft.com/en-us/library/live/microsoft.live.liveauthclient.aspx – Adarsha 2012-10-07 03:54:22

1

感謝您的代碼示例 - 幫我想出的代碼更新版本的Windows Phone 8等:)


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

此代碼給出以下錯誤 - mscorlib.ni.dll中發生類型爲「Microsoft.Live.LiveAuthException」的異常,但未在用戶代碼中處理 – 2013-12-10 11:59:38

10

大答案理查德。這真的很有幫助。

我注意到一些人抱怨他們找不到InitializedCompleted事件的評論。如果您使用.Net 4.5進行編碼,那麼您需要按照異步/等待模式進行異步方法。上面的類是這樣的:

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有一個異步等待底漆here