private void InitializeView() { // Create the Facebook LogIn View with the needed Permissions // https://developers.facebook.com/ios/login-ui-control/ var loginView = new FBLoginView() { Frame = new RectangleF(85, 0, 151, 43) }; // Hook up to FetchedUserInfo event, so you know when // you have the user information available bool didFetchUserInfo = false; loginView.FetchedUserInfo += (sender, e) => { if (didFetchUserInfo) { return; } didFetchUserInfo = true; Console.WriteLine("User is logged in ID=" + e.User.GetId() + ", User name is " + e.User.GetName()); FacebookClient fb = new FacebookClient(FBSession.ActiveSession.AccessTokenData.AccessToken); fb.AppId = PopPicConstants.AppId; fb.AppSecret = PopPicConstants.AppSecret; BuddyClient buddyClient = new BuddyClient(PopPicConstants.BuddyAppName, PopPicConstants.BuddyAppKey); buddyClient.SocialLoginAsync("Facebook", e.User.GetId(), FBSession.ActiveSession.AccessTokenData.AccessToken).ContinueWith(result => { var authenticatedUser = result.Result; var repository = new GameRepository(authenticatedUser, buddyClient, fb); AppDelegate.Repository = repository; InvokeOnMainThread(() => { if (this.ReturnAfterLoading) { this.NavigationController.PopViewControllerAnimated(true); } if (this.RepositoryLoaded != null) { this.RepositoryLoaded(this, repository); } }); }); }; // Clean user Picture and label when Logged Out loginView.ShowingLoggedOutUser += (sender, e) => { }; Root = new RootElement("Log In") { new Section("Please Sign In") { new UIViewElement("", loginView, false) { Flags = UIViewElement.CellFlags.DisableSelection | UIViewElement.CellFlags.Transparent, } } }; }
private void AuthThroughBuddy(string fbUserId, string fbUserToken) { //Check to ensure everything's setup right GcmClient.CheckDevice(this); GcmClient.CheckManifest(this); GcmClient.Register(this, GcmBroadcastReceiver.SENDER_IDS); var registrationId = GcmClient.GetRegistrationId(this); FacebookClient fb = new FacebookClient(fbUserToken); fb.AppId = AppId; fb.AppSecret = AppSecret; BuddyClient client = new BuddyClient(BuddyAppName, BuddyAppKey); const string BuddyAccountKey = "BuddyAccount"; const string BuddyAccessTokenKey = "AccessToken"; const string FacebookIDTokenKey = "FacebookIdTokenKey"; var accountStore = AccountStore.Create(this); var savedAccount = accountStore.FindAccountsForService(BuddyAccountKey).LastOrDefault(); Task <AuthenticatedUser> getUserTask; bool saveAccount = false; if (savedAccount != null && savedAccount.Properties.ContainsKey(BuddyAccessTokenKey)) { saveAccount = false; var token = savedAccount.Properties [BuddyAccessTokenKey]; getUserTask = client.LoginAsync(token); } else { saveAccount = true; getUserTask = client.SocialLoginAsync("Facebook", fbUserId, fbUserToken).ContinueWith((u) => { if (u.IsFaulted) { // try again for kicks } return((AuthenticatedUser)u.Result); }); } getUserTask.ContinueWith(r => { Console.WriteLine("Get User Task has happened result is faulted = " + r.IsFaulted.ToString()); if (!r.IsFaulted) { AuthenticatedUser user = r.Result; var successActivity = new Action(() => { var repository = new POPpicLibrary.GameRepository(user, client, fb); Console.WriteLine("Success task is running!"); if (saveAccount) { var properties = new Dictionary <string, string>(); properties[BuddyAccessTokenKey] = user.Token; properties[FacebookIDTokenKey] = fbUserId; Account buddyAccount = new Account(user.ID.ToString(), properties); accountStore.Save(buddyAccount, BuddyAccountKey); } // Finish(); if (progressDialog != null) { progressDialog.Dismiss(); } ((POPpicApplication)Application).SetGameRepository(repository); }); if (string.IsNullOrEmpty(user.ApplicationTag)) { user.PhotoAlbums.CreateAsync(user.ID.ToString(), true, null).ContinueWith(pa => { if (!pa.IsFaulted) { var album = pa.Result; user.VirtualAlbums.CreateAsync(user.ID.ToString(), null).ContinueWith(va => { if (!va.IsFaulted) { var virtualAlbum = va.Result; var extraData = new UserExtraData(); extraData.UploadAlbumId = album.AlbumId; extraData.WinnerAblumVirtualId = virtualAlbum.ID; user.UpdateAsync(null, null, UserGender.Any, 0, null, UserStatus.Any, false, false, JsonConvert.SerializeObject(extraData)).ContinueWith(updateResult => { if (!updateResult.IsFaulted && updateResult.Result) { RunOnUiThread(successActivity); } }); } }); } }); } else { RunOnUiThread(successActivity); } } else { if (progressDialog != null) { progressDialog.Dismiss(); } RunOnUiThread(() => { AndroidUtilities.ShowAlert(this, "Error Getting User Account", r.Exception.Message, "Try Again", (object sender, DialogClickEventArgs e) => { AuthThroughBuddy(); }); }); Console.WriteLine(r.Exception.Message); } }); }