private void HandleAuthenticate(bool success) { Debug.Log("*** HandleAuthenticated: success = " + success); if (success) { loginTupe = SocialLoginType.success; //需要在iTunesConnect里设置排行、成就之类的东西 //Social.localUser.LoadFriends(HandleFriendsLoaded); //Social.LoadAchievements(HandleAchievementsLoaded); //Social.LoadAchievementDescriptions(HandleAchievementDescriptionsLoaded); } else { loginTupe = SocialLoginType.failure; } }
/// <summary> /// Tries the authentication with a social network. /// </summary> /// <param name="socialNetwork">The social network.</param> /// <param name="token">The token.</param> /// <param name="token2">The token2.</param> /// <returns> /// Tuple with a boolean if is true the user already existed and the user authenticated. /// </returns> /// <exception cref="AdoptersException"> /// Error trying to login /// </exception> public async Task <Tuple <bool, User> > TryAuthenticate(SocialLoginType socialNetwork, string token, string token2) { bool userExisted = false; string socialId = string.Empty; string email = string.Empty; string name = string.Empty; ////Intenta realizar la autenticación por cualquiera de las redes y actualiza los datos switch (socialNetwork) { case SocialLoginType.Facebook: var facebookUser = await this.socialAuthenticationService.GetFacebookUser(token); if (string.IsNullOrEmpty(facebookUser.Error)) { socialId = facebookUser.Id.ToString(); email = facebookUser.Email; name = facebookUser.Name; } else { throw new AdoptersException(AdopterExceptionCode.ErrorTryingExternalLogin, facebookUser.Error); } break; default: throw new AdoptersException(AdopterExceptionCode.InvalidExternalAuthenticationProvider); } User user = null; switch (socialNetwork) { case SocialLoginType.Facebook: user = this.userRepository.Table.FirstOrDefault(u => u.FacebookId == socialId); break; } ////Si el usuario ya está registrado lo retorna if (user != null) { userExisted = true; return(Tuple.Create <bool, User>(userExisted, user)); } else { userExisted = false; ////Consulta para validar si el usuario ya está registrado previamente con el mismo correo para asociarle la red ////Unicamente cuando el correo no es nulo ni vacio if (!string.IsNullOrEmpty(email)) { user = this.userRepository.Table.FirstOrDefault(c => c.Email.Equals(email)); } bool toCreate = user == null; ////Si el usuario definitivamente no existe lo crea if (user == null) { ////Crea un objeto base para ser guardado user = new User() { Name = name, Email = email, Role = Role.Public }; toCreate = true; } switch (socialNetwork) { case SocialLoginType.Facebook: user.FacebookId = socialId; break; default: break; } if (toCreate) { userExisted = false; await this.userService.Insert(user); } else { userExisted = true; await this.userService.Update(user); } return(Tuple.Create <bool, User>(userExisted, user)); } }
public async Task <SocialUserViewModel> GetSocialUserData(string access_token, SocialLoginType socialLoginType) { try { HttpClient client = new HttpClient(); string urlProfile = ""; if (socialLoginType == SocialLoginType.Google) { urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + access_token; } else if (socialLoginType == SocialLoginType.Facebook) { urlProfile = "https://graph.facebook.com/me?access_token=" + access_token; } client.CancelPendingRequests(); HttpResponseMessage output = await client.GetAsync(urlProfile); if (output.IsSuccessStatusCode) { string outputData = await output.Content.ReadAsStringAsync(); SocialUserViewModel socialUser = JsonConvert.DeserializeObject <SocialUserViewModel>(outputData); return(socialUser); //if (socialUser != null) //{ // // You will get the user information here. //} } return(null); } catch (Exception ex) { throw ex; } }
public async Task <DunkeyAPI.SocialUserViewModel> GetSocialUserData(string access_token, SocialLoginType socialLoginType) { try { HttpClient client = new HttpClient(); string urlProfile = ""; string picture = ""; if (socialLoginType == SocialLoginType.Google) { urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + access_token; } else if (socialLoginType == SocialLoginType.Facebook) { urlProfile = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name&access_token=" + access_token; } client.CancelPendingRequests(); HttpResponseMessage output = await client.GetAsync(urlProfile); if (output.IsSuccessStatusCode) { string outputData = await output.Content.ReadAsStringAsync(); SocialUserViewModel socialUser = JsonConvert.DeserializeObject <SocialUserViewModel>(outputData); socialUser.picture = "http://graph.facebook.com/" + socialUser.id + "/picture?type=large"; return(socialUser); } return(null); } catch (Exception ex) { throw ex; } }
public async Task <SocialUserViewModel> GetSocialUserData(string access_token, SocialLoginType socialLoginType) { try { HttpClient client = new HttpClient(); string urlProfile = ""; string picture = ""; if (socialLoginType == SocialLoginType.Google) { urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + access_token; } else if (socialLoginType == SocialLoginType.Facebook) { // urlProfile = "https://graph.facebook.com/me?access_token=" + access_token; urlProfile = "https://graph.facebook.com/v2.4/me?fields=id,name,email,first_name,last_name&access_token=" + access_token; } else if (socialLoginType == SocialLoginType.Twitter) { } client.CancelPendingRequests(); HttpResponseMessage output = await client.GetAsync(urlProfile); if (output.IsSuccessStatusCode) { string outputData = await output.Content.ReadAsStringAsync(); SocialUserViewModel socialUser = JsonConvert.DeserializeObject <SocialUserViewModel>(outputData); socialUser.picture = "http://graph.facebook.com/" + socialUser.id + "/picture?type=large"; if (!String.IsNullOrEmpty(socialUser.id)) { socialUser.username = socialUser.id.ToString(); } socialUser.password = socialUser.email; return(socialUser); //if (socialUser != null) //{ // // You will get the user information here. //} } return(null); } catch (Exception ex) { throw ex; } }