2016-01-20 108 views
2

如何從IOneDriveClient獲取用戶名或電子郵件?如何從onedriveclient sdk獲取用戶名或電子郵件c#

認證:

string[] scopes = { "onedrive.readwrite" }; 
IOneDriveClient OneDriveClient = OneDriveClientExtensions.GetUniversalClient(scopes); 
await OneDriveClient.AuthenticateAsync(); 
+0

如何獲得用戶帳戶的圖片 –

+0

@DeviPrasad //獲得由電話號碼 接觸異步任務 _Get_Contact_ByPhone(串TEL) { var contactStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly); var contacts = await contactStore.FindContactsAsync(tel); return contacts.Count> 0? contacts.ElementAt(0):null; } 然後使用'SmallDisplayPicture'或'LargeDisplayPicture'字段獲取流。 對不起,標記。 – jonsbox

回答

5

我們不能從IOneDriveClient直接獲取用戶名或電子郵件。但形式IOneDriveClient我們可以得到AccessToken。當我們有AccessToken時,我們可以將它與Live Connect具象狀態傳輸(REST)API一起使用來檢索用戶的名稱。

GET https://apis.live.net/v5.0/me?access_token=ACCESS_TOKEN 

欲瞭解更多資訊,請Requesting info using REST

的REST API來對登錄的用戶請求信息。

所以在應用中,我們可以使用下面的代碼來獲取用戶的顯示名稱:

string[] scopes = new string[] { "onedrive.readwrite" }; 
var client = OneDriveClientExtensions.GetUniversalClient(scopes) as OneDriveClient; 
await client.AuthenticateAsync(); 
//get the access_token 
var AccessToken = client.AuthenticationProvider.CurrentAccountSession.AccessToken; 
//REST API to request info about the signed-in user 
var uri = new Uri($"https://apis.live.net/v5.0/me?access_token={AccessToken}"); 

var httpClient = new System.Net.Http.HttpClient(); 
var result = await httpClient.GetAsync(uri); 
//user info returnd as JSON 
string jsonUserInfo = await result.Content.ReadAsStringAsync(); 
if (jsonUserInfo != null) 
{ 
    var json = Newtonsoft.Json.Linq.JObject.Parse(jsonUserInfo); 
    string username = json["name"].ToString(); 
} 

要獲得用戶的電子郵件,我們需要在scopes添加wl.emails範圍。 wl.emails scope可以讀取用戶的電子郵件地址。的代碼可能會喜歡以下:

string[] scopes = new string[] { "onedrive.readwrite", "wl.emails" }; 
var client = OneDriveClientExtensions.GetUniversalClient(scopes) as OneDriveClient; 
await client.AuthenticateAsync(); 
//get the access_token 
var AccessToken = client.AuthenticationProvider.CurrentAccountSession.AccessToken; 
//REST API to request info about the signed-in user 
var uri = new Uri($"https://apis.live.net/v5.0/me?access_token={AccessToken}"); 

var httpClient = new System.Net.Http.HttpClient(); 
var result = await httpClient.GetAsync(uri); 
//user info returnd as JSON 
string jsonUserInfo = await result.Content.ReadAsStringAsync(); 
if (jsonUserInfo != null) 
{ 
    var json = Newtonsoft.Json.Linq.JObject.Parse(jsonUserInfo); 
    string username = json["name"].ToString(); 
    string email = json["emails"]["account"].ToString(); 
} 
+0

我們怎樣才能得到用戶的照片。 –

+0

@DeviPrasad //通過電話號碼獲取聯繫人async任務 _Get_Contact_ByPhone(string tel){var contactStore = await ContactManager.RequestStoreAsync(ContactStoreAccessType.AllContactsReadOnly); var contacts = await contactStore.FindContactsAsync(tel);返回contacts.Count> 0? contacts.ElementAt(0):null; }然後使用SmallDisplayPicture或LargeDisplayPicture字段獲取流。對不起,標記。 – jonsbox

相關問題