private static async void OnAuthLinkedCompleted(object sender, AuthenticatorCompletedEventArgs e) { var authenticator = sender as OAuth2Authenticator; if (authenticator != null) { authenticator.Completed -= OnAuthLinkedCompleted; authenticator.Error -= OnAuthLinkedError; } //LoginUser = null; if (e.IsAuthenticated) { // If the user is authenticated, request their basic user data from Google // UserInfoUrl = https://www.googleapis.com/oauth2/v2/userinfo var request = new OAuth2Request("GET", new Uri(UserInfoUrl), null, e.Account); var response = await request.GetResponseAsync(); if (response != null) { // Deserialize the data and store it in the account store // The users email address will be used to identify data in SimpleDB string userJson = await response.GetResponseTextAsync(); GoogleUser googleUser = JsonConvert.DeserializeObject <GoogleUser>(userJson); var ggUser = new User() { FullName = googleUser.Name, Email = googleUser.Email, GoogleId = googleUser.Id, AvatarUrl = googleUser.Picture }; try { ApiResponse apiResponse = await ApiHelper.Put("api/auth/sociallinked", ggUser, true); if (apiResponse.IsSuccess) { var user = JsonConvert.DeserializeObject <User>(apiResponse.Content.ToString()); UserLogged.SaveProfile(user); await Application.Current.MainPage.DisplayAlert("", Language.lien_ket_google_thanh_cong, Language.dong); //MessagingCenter.Send<GoogleHelper, bool>(this, "UpdateSocialLinked", true); } else { throw new Exception(apiResponse.Message); } } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("", Language.loi_he_thong_vui_long_thu_lai, Language.dong); } } } }
private async void WebView_NavigatingSocialLinked(object sender, WebNavigatedEventArgs e) { //gridLoading.IsVisible = false; if (e.Url.StartsWith("https://bsdinsight.net", StringComparison.Ordinal)) { try { string queryString = e.Url.Replace(CallBackUrl, ""); Dictionary <string, string> keyValues = ParseQueryString(queryString); if (keyValues.ContainsKey("code") == false) { return; } string code = keyValues["code"]; using (HttpClient client = new HttpClient()) { HttpResponseMessage response = await client.GetAsync($"https://oauth.zaloapp.com/v3/access_token?app_id={ZALOAPPID}&app_secret={AppScret}&code={code}"); if (response.IsSuccessStatusCode) { HttpContent auContent = response.Content; string authBody = await auContent.ReadAsStringAsync(); ZaloAuthReponse zaloAuthReponse = JsonConvert.DeserializeObject <ZaloAuthReponse>(authBody); var profileResponse = await client.GetAsync($"https://graph.zalo.me/v2.0/me?access_token={zaloAuthReponse.access_token}&fields=id,birthday,name,gender,picture"); var profileBody = await profileResponse.Content.ReadAsStringAsync(); ZaloUser zaloUser = JsonConvert.DeserializeObject <ZaloUser>(profileBody); var zlUser = new User() { ZaloId = long.Parse(zaloUser.id), FullName = zaloUser.name, Sex = zaloUser.gender == "male" ? (short)0 : zaloUser.gender == "female" ? (short)1 : (short)-1, Birthday = DateTime.ParseExact(zaloUser.birthday, "dd/MM/yyyy", null), AvatarUrl = zaloUser.picture.data.url }; try { ApiResponse apiResponse = await ApiHelper.Put("api/auth/sociallinked", zlUser, true); if (apiResponse.IsSuccess) { var user = JsonConvert.DeserializeObject <User>(apiResponse.Content.ToString()); UserLogged.SaveProfile(user); viewModel.IsZaloLoginVisible = false; await Application.Current.MainPage.DisplayAlert("", Language.lien_ket_zalo_thanh_cong, Language.dong); MessagingCenter.Send <SocialLinkedPage, bool>(this, "UpdateSocialLinked", true); } else { throw new Exception(apiResponse.Message); } } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("", ex.Message, Language.dong); await Navigation.PopAsync(); } } else { await DisplayAlert("", Language.loi_he_thong_vui_long_thu_lai, Language.dong); await this.Navigation.PopToRootAsync(); } } } catch (Exception ex) { await DisplayAlert("", Language.loi_he_thong_vui_long_thu_lai, Language.dong); await this.Navigation.PopToRootAsync(); } } }
public static async Task FacebookLinked() { try { if (_facebookService.IsLoggedIn) { _facebookService.Logout(); } EventHandler <FBEventArgs <string> > userDataDelegate = null; userDataDelegate = async(object sender, FBEventArgs <string> e) => { if (e == null) { return; } //switch (e.Status) //{ // case FacebookActionStatus.Completed: // break; // case FacebookActionStatus.Canceled: // break; //} _facebookService.OnUserData -= userDataDelegate; }; _facebookService.OnUserData += userDataDelegate; string[] fbRequestFields = { "email", "first_name", "gender", "last_name", "picture.width(500).height(500)" }; string[] fbPermisions = { "email" }; var res = await _facebookService.RequestUserDataAsync(fbRequestFields, fbPermisions); var fbProfile = await Task.Run(() => JsonConvert.DeserializeObject <FacebookUser>(res.Data)); var fbUser = new User() { FullName = fbProfile.FirstName + " " + fbProfile.LastName, Email = fbProfile.Email, FacebookId = long.Parse(fbProfile.Id), AvatarUrl = fbProfile.Picture.Data.Url }; //check exist try { ApiResponse response = await ApiHelper.Put("api/auth/sociallinked", fbUser, true); if (response.IsSuccess) { var user = JsonConvert.DeserializeObject <User>(response.Content.ToString()); UserLogged.SaveProfile(user); await Application.Current.MainPage.DisplayAlert("Thông báo", Language.lien_ket_facebook_thanh_cong, Language.dong); } else { throw new Exception(response.Message); } } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("Thông báo", ex.Message, Language.dong); } } catch (Exception ex) { Debug.WriteLine(ex.ToString()); } }