/// <summary> /// 根據通過身分驗證的資訊(如 存取權杖),取得該使用者的明細資訊 /// </summary> /// <param name="account"></param> /// <returns></returns> public static async Task FetchUserProfile(Account account) { // 取得 Prism 相依性服務使用到的容器 IUnityContainer fooContainer = (XFoAuth2.App.Current as PrismApplication).Container; // 取得 IAccountStore 介面實際實作的類別物件 IAccountStore fooIAccountStore = fooContainer.Resolve <IAccountStore>(); if (AuthenticationHelper.OAuthType == OAuthTypeEnum.Google) { // 取得 OAuth2 需要用到的參數定義物件 var fooOAuthParas = OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Google); // 取得使用者的詳細資訊 var request = new OAuth2Request("GET", new Uri(fooOAuthParas.UserInfoUrl), null, account); var response = await request.GetResponseAsync(); if (response != null) { // 取得回傳的 JSON 格式文字 var userJson = response.GetResponseText(); var user = JsonConvert.DeserializeObject <GoogleUserProfile>(userJson); var foo = user; #region 將認證通過的相關使用者明細資訊,儲存到 Account 物件內 account.Properties[Constants.IDAccountProperty] = user.Id; account.Properties[Constants.NameAccountProperty] = user.Name; account.Properties[Constants.EmailAccountProperty] = user.Email; account.Properties[Constants.PhotoAccountProperty] = user.Picture; account.Properties[Constants.LoginTypeAccountProperty] = AuthenticationHelper.OAuthType.ToString(); #endregion #region 將通過認證的 OAuth2 帳號與使用者詳細資訊,儲存到本機上(其中 Xamarin.Auth 並不支援 UWP,所以,使用相依性注入服務來解決 if (fooIAccountStore.GetPlatform() == "UWP") { await fooIAccountStore.SaveAccount(account); } else { AccountStore.Create().Save(account, AuthenticationHelper.AppName); } #endregion } } else { // 取得 OAuth2 需要用到的參數定義物件 var fooOAuthParas = OAuthParas.FirstOrDefault(x => x.Type == OAuthTypeEnum.Facebook); // 取得使用者的詳細資訊 var request = new OAuth2Request("GET", new Uri(fooOAuthParas.UserInfoUrl), null, account); var response = await request.GetResponseAsync(); if (response != null) { // 取得回傳的 JSON 格式文字 var userJson = response.GetResponseText(); string fooJson = userJson; var user = JsonConvert.DeserializeObject <FacebookUserProfile>(userJson); var foo = user; #region 將認證通過的相關使用者明細資訊,儲存到 Account 物件內 account.Properties[Constants.IDAccountProperty] = user.id; account.Properties[Constants.NameAccountProperty] = $"{user.first_name} {user.name}"; account.Properties[Constants.EmailAccountProperty] = user.email; account.Properties[Constants.PhotoAccountProperty] = user.picture.data.url; account.Properties[Constants.LoginTypeAccountProperty] = AuthenticationHelper.OAuthType.ToString(); #endregion #region 將通過認證的 OAuth2 帳號與使用者詳細資訊,儲存到本機上(其中 Xamarin.Auth 並不支援 UWP,所以,使用相依性注入服務來解決 if (fooIAccountStore.GetPlatform() == "UWP") { await fooIAccountStore.SaveAccount(account); } else { AccountStore.Create().Save(account, AuthenticationHelper.AppName); } #endregion } } }