public async Task <TRUser> FacebookLogin(FBUserInfo fbUserInfo) { TRUser result = null; string response = String.Empty; MvxTrace.Trace("Facebook Loing with email:{0}", fbUserInfo.UserEmail); fbUserInfo.Provider = TRConstant.TRFBOAuthProvider; string serialized = JsonConvert.SerializeObject(fbUserInfo); try { response = await CallRestAPI(loginMethod, JsonConvert.SerializeObject(fbUserInfo)); result = JsonConvert.DeserializeObject <TRUser>(response, jsonSerializeSetting); } catch (Exception ex) { Xamarin.Insights.Report(ex, new Dictionary <string, string> { { "Exception Time", DateTime.Now.ToString() }, { "Description", "Parse Failed during login" }, { "Target String", response }, }, Xamarin.Insights.Severity.Error); MvxTrace.Trace("Parse Failed during Loing API Target:{0}", response); } return(result); }
/// <summary> /// Gets (from Facebook API) and sets (in to CurrentUser) info about the user /// (i.e Name and FB User ID) from a Facebook API call /// </summary> /// <param name="callbackResult">Result of a FB API Call for ("/me")</param> private static void UserInfoCallback(IGraphResult callbackResult) { //Create a dictonary of the result that we got back from the API Call Dictionary <string, object> callbackResultsDictionary = (Dictionary <string, object>)Facebook.MiniJSON.Json.Deserialize(callbackResult.RawResult); //Check that we have got the right call back (i.e it contains a key for name and user ID) if (!callbackResultsDictionary.ContainsKey("name") || !callbackResultsDictionary.ContainsKey("id")) { Debug.LogWarning("UserInfoCallback was called with the wrong call back info"); //Trigger Event for unsuccessful login and log out the current user FBLoginEvent?.Invoke(LoginEventResult.LoginFailed); FacebookLogout(); return; } //We get the name and userID back from our API call. (Name: "", userID: "") //we need to put this in our user info class CurrentUser = new FBUserInfo((string)callbackResultsDictionary["name"], Convert.ToInt64(callbackResultsDictionary["id"])); //Trigger event for successful logon FBLoginEvent?.Invoke(LoginEventResult.LoginSuccess); }
private async void ProcessFacebookLogin() { LoginManagerLoginResult result = await loginManager.LogInWithReadPermissionsAsync(readPermissions.ToArray(), this); if (result.IsCancelled) { UserDialogs.Instance.Alert("User cancelled Facebook Login", "ThisRoof"); return; } if (result.GrantedPermissions != null && result.GrantedPermissions.Contains("email")) { string NAME = "name"; string ID = "id"; string EMAIL = "email"; string FIELDS = "fields"; string REQUEST_FIELDS = String.Join(",", new string[] { NAME, ID, EMAIL }); UserDialogs.Instance.ShowLoading(); var request = new GraphRequest("me", new NSDictionary(FIELDS, REQUEST_FIELDS), AccessToken.CurrentAccessToken.TokenString, null, "GET"); var requestConnection = new GraphRequestConnection(); requestConnection.AddRequest(request, (connection, graphResult, error) => { UserDialogs.Instance.HideLoading(); if (error != null || graphResult == null) { // Error Handler UserDialogs.Instance.Alert(String.Format("Facebook Login Error:{0}", error.Description), "ThisRoof"); } else { string email = (graphResult as NSDictionary) ["email"].ToString(); FBUserInfo userInfo = new FBUserInfo() { UserEmail = email, UserID = AccessToken.CurrentAccessToken.UserID, UserToken = AccessToken.CurrentAccessToken.TokenString }; ViewModelInstance.FacebookLoginCommand.Execute(userInfo); // after get user info we logout again. loginManager.LogOut(); } }); requestConnection.Start(); } else { UserDialogs.Instance.Alert("Email Permission is not allowed", "ThisRoof"); } }
private async void DoFacebookSignup(FBUserInfo fbUserInfo) { UserDialogs.Instance.ShowLoading(); TRUser user = await m_TRService.FacebookLogin(fbUserInfo); UserDialogs.Instance.HideLoading(); if (user != null && user.Success) { TRService.Token = user.AccessToken; // Goto Home screen MvxTrace.Trace("Facebook Signup success:{0}", user.AccessToken); } else { UserDialogs.Instance.Alert("Failed to signup using Facebook. Please contact administrator", "ThisRoof", "OK"); } }