Exemplo n.º 1
0
        public override void ViewWillAppear(bool animated)
        {
            base.ViewWillAppear(animated);

            // Update UI based on state
            SendTweet.Enabled = isTwitterAvailable;
            RequestTwitterTimeline.Enabled  = false;
            PostToFacebook.Enabled          = isFacebookAvailable;
            RequestFacebookTimeline.Enabled = false;

            // Initialize Twitter Account access
            var accountStore = new ACAccountStore();
            var accountType  = accountStore.FindAccountType(ACAccountType.Twitter);

            // Request access to Twitter account
            accountStore.RequestAccess(accountType, null, (granted, error) => {
                // Allowed by user?
                if (granted)
                {
                    // Get account
                    if (accountStore.Accounts.Length == 0)
                    {
                        return;
                    }

                    twitterAccount = accountStore.Accounts [accountStore.Accounts.Length - 1];
                    InvokeOnMainThread(() => {
                        // Update UI
                        RequestTwitterTimeline.Enabled = true;
                    });
                }
            });

            // Initialize facebook Account access
            var options = new AccountStoreOptions();

            options.FacebookAppId = "";             // Enter your specific Facebook App ID here
            accountType           = accountStore.FindAccountType(ACAccountType.Facebook);

            // Request access to Facebook account
            accountStore.RequestAccess(accountType, options, (granted, error) => {
                // Allowed by user?
                if (granted)
                {
                    if (accountStore.Accounts.Length == 0)
                    {
                        return;
                    }

                    // Get account
                    facebookAccount = accountStore.Accounts [accountStore.Accounts.Length - 1];
                    InvokeOnMainThread(() => {
                        // Update UI
                        RequestFacebookTimeline.Enabled = true;
                    });
                }
            });
        }
Exemplo n.º 2
0
        ACAccountStore accountStore;         // Save this reference since ACAccounts are only good so long as it's alive

        public override Task <IEnumerable <Account> > GetAccountsAsync()
        {
            if (accountStore == null)
            {
                accountStore = new ACAccountStore();
            }
            var store = new ACAccountStore();
            var at    = store.FindAccountType(ACAccountType.Twitter);

            var tcs = new TaskCompletionSource <IEnumerable <Account> > ();

            store.RequestAccess(at, (granted, error) => {
                if (granted)
                {
                    var accounts = store.FindAccounts(at)
                                   .Select(a => (Account) new ACAccountWrapper(a, store))
                                   .ToList();

                    tcs.SetResult(accounts);
                }
                else
                {
                    tcs.SetResult(new Account [0]);
                }
            });

            return(tcs.Task);
        }
        private Tuple <string, string> verifyFacebookFromSerttings()
        {
            Tuple <string, string> signedInResult = Tuple.Create <string, string>(string.Empty, string.Empty);
            AutoResetEvent         taskCompleted  = new AutoResetEvent(false);

            var options = new AccountStoreOptions()
            {
                FacebookAppId = "1626108114272416"
            };

            options.SetPermissions(ACFacebookAudience.Friends, new[] { "public_profile", "email" });

            ACAccountStore accountStore = new ACAccountStore();
            var            accountType  = accountStore.FindAccountType(ACAccountType.Facebook);

            var facebookAccounts = accountStore.FindAccounts(accountType);

            if ((facebookAccounts == null) || (facebookAccounts.Length == 0))
            {
                accountStore.RequestAccess(accountType, options, (granted, error2) =>
                {
                    if (granted)
                    {
                        facebookAccounts = accountStore.FindAccounts(accountType);
                    }

                    taskCompleted.Set();
                });

                taskCompleted.WaitOne();
            }

            if ((facebookAccounts != null) && (facebookAccounts.Length > 0))
            {
                var facebookAccount = facebookAccounts[0];
                accountStore.RenewCredentials(facebookAccount, (result, error1) =>
                {
                    if (result == ACAccountCredentialRenewResult.Renewed)
                    {
                        var id = parseFacebookUserIdFromSettings(facebookAccount);
                        if (!string.IsNullOrEmpty(id))
                        {
                            signedInResult = Tuple.Create <string, string>(facebookAccount.Credential.OAuthToken, id);
                        }
                    }

                    taskCompleted.Set();
                });
            }
            else
            {
                taskCompleted.Set();
            }

            taskCompleted.WaitOne();
            return(signedInResult);
        }
        public Task RequestAccess()
        {
            AccountStoreOptions options = GetOptions();
            ACAccountType       account = accountStore.FindAccountType(socialNetwork);

            var tcs = new TaskCompletionSource <object> ();

            accountStore.RequestAccess(account, options, (granted, error) => tcs.SetResult(null));
            return(tcs.Task);
        }
Exemplo n.º 5
0
        public void RequestFacebookAccess()
        {
            if (accountStore == null)
            {
                accountStore = new ACAccountStore();
            }

            ACAccountType facebookAccount = accountStore.FindAccountType(ACAccountType.Facebook);

            AccountStoreOptions options = new AccountStoreOptions()
            {
                FacebookAppId = "MY_CODE"
            };

            options.SetPermissions(ACFacebookAudience.Friends, new [] { "email", "user_about_me" });

            accountStore.RequestAccess(facebookAccount, options, delegate(bool granted, NSError error) {
                ShowAlert(DataClass.Facebook, granted ? "granted" : "denied");
            });
        }
Exemplo n.º 6
0
 private void HandleTwitterAuth(object sender, EventArgs args)
 {
     if (HardwareDetection.RunningiOS5OrHigher)
     {
         //if we are running iOS5 or later, we can use the in-built API for handling twitter authentication.
         accountStore = new ACAccountStore();
         accountStore.RequestAccess(accountStore.FindAccountType(ACAccountType.Twitter), retrievedAccounts);
     }
     else
     {
         HandleTwitterOAuth();
     }
 }
Exemplo n.º 7
0
		public void Manual_HasMultipleAccounts ()
		{
			var store = new ACAccountStore ();
			var at = store.FindAccountType (ACAccountType.Twitter);

			store.RequestAccess (at, (granted, error) => {

				if (granted) {
					var accounts = store.FindAccounts (at);

					Assert.That (accounts.Length, Is.GreaterThanOrEqualTo (2));
				}
			});
		}
Exemplo n.º 8
0
        public void Manual_HasMultipleAccounts()
        {
            var store = new ACAccountStore();
            var at    = store.FindAccountType(ACAccountType.Twitter);

            store.RequestAccess(at, (granted, error) => {
                if (granted)
                {
                    var accounts = store.FindAccounts(at);

                    Assert.That(accounts.Length, Is.GreaterThanOrEqualTo(2));
                }
            });
        }
        public static Task <bool> RequestAccessAsync(this ACAccountStore store, ACAccountType accountType, AccountStoreOptions options)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool> ();

            store.RequestAccess(accountType, options, delegate(bool granted, NSError error)
            {
                if (error != null)
                {
                    tcs.SetException(new NSErrorException(error));
                }
                else
                {
                    tcs.SetResult(granted);
                }
            });
            return(tcs.Task);
        }
Exemplo n.º 10
0
        public static void getAccount()
        {
            if (twitterAccount != null)
                return;

            ACAccountStore accountStore = new ACAccountStore();
            ACAccountType accountTypeTwitter = accountStore.FindAccountType(ACAccountType.Twitter);
            accountStore.RequestAccess(accountTypeTwitter, (granted, error) =>
            {
                if (!granted)
                    return;

                ACAccount[] accounts = accountStore.FindAccounts(accountTypeTwitter);
                if (accounts.Length > 0)
                    twitterAccount = accounts[0];
            });
        }
Exemplo n.º 11
0
			public override void ViewWillAppear (bool animated)
			{
				base.ViewWillAppear (animated);

				// Update UI based on state
				SendTweet.Enabled = isTwitterAvailable;
				RequestTwitterTimeline.Enabled = false;
				PostToFacebook.Enabled = isFacebookAvailable;
				RequestFacebookTimeline.Enabled = false;

				// Initialize Twitter Account access 
				var accountStore = new ACAccountStore ();
				var accountType = accountStore.FindAccountType (ACAccountType.Twitter);

				// Request access to Twitter account
				accountStore.RequestAccess (accountType, (granted, error) => {
					// Allowed by user?
					if (granted) {
						// Get account
						_twitterAccount = accountStore.Accounts [accountStore.Accounts.Length - 1];
						InvokeOnMainThread (() => {
							// Update UI
							RequestTwitterTimeline.Enabled = true;
						});
					}
				});

				// Initialize facebook Account access 
				var options = new AccountStoreOptions ();
				options.FacebookAppId = ""; // Enter your specific Facebook App ID here
				accountType = accountStore.FindAccountType (ACAccountType.Facebook);

				// Request access to Facebook account
				accountStore.RequestAccess (accountType, options, (granted, error) => {
					// Allowed by user?
					if (granted) {
						// Get account
						_facebookAccount = accountStore.Accounts [accountStore.Accounts.Length - 1];
						InvokeOnMainThread (() => {
							// Update UI
							RequestFacebookTimeline.Enabled = true;
						});
					}
				});

			}
        public void TestFacebookAuth()
        {
            var doneEvent = new ManualResetEvent(false);

            var accountStore = new ACAccountStore();
            var accountType = accountStore.FindAccountType(ACAccountType.Facebook);

            var options = new AccountStoreOptions();
            options.FacebookAppId = FacebookAppId;
            options.SetPermissions(ACFacebookAudience.Friends, new [] { "email" });

            var success = true;
            ACAccount account = null;
            accountStore.RequestAccess(accountType, options, (result, error) => 
            {
                success = result;
                if (success)
                {
                    var accounts = accountStore.FindAccounts(accountType);
                    account = accounts != null && accounts.Length > 0 ? accounts[0] : null;
                }
                else
                {
                    Log.W(Tag, "Facebook Login needed. Go to Settings > Facebook and login.");
                    Log.E(Tag, "Facebook Request Access Error : " + error);
                }
                doneEvent.Set();
            });

            doneEvent.WaitOne(TimeSpan.FromSeconds(30));

            Assert.IsTrue(success);
            Assert.IsNotNull(account);

            var token = account.Credential.OAuthToken;
            Assert.IsNotNull(token);
            Assert.IsTrue(token.Length > 0);

            var url = GetReplicationURLWithoutCredentials();

            var cookieStore = new CookieStore();
            var httpClientFactory = new CouchbaseLiteHttpClientFactory(cookieStore);
            manager.DefaultHttpClientFactory = httpClientFactory;
            Replication replicator = database.CreatePushReplication(url);
            replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(token);

            Assert.IsNotNull(replicator);
            Assert.IsNotNull(replicator.Authenticator);
            Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);

            replicator.Start();

            doneEvent.Reset();
            Task.Factory.StartNew(()=>
            {
                var timeout = DateTime.UtcNow + TimeSpan.FromSeconds(30);
                while (DateTime.UtcNow < timeout)
                {
                    if (!replicator.active)
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(10));
                }
                doneEvent.Set();
            });
            doneEvent.WaitOne(TimeSpan.FromSeconds(35));

            var urlStr = url.ToString();
            urlStr = urlStr.EndsWith("/") ? urlStr : urlStr + "/";
            var cookies = httpClientFactory.GetCookieContainer().GetCookies(new Uri(urlStr));
            Assert.IsTrue(cookies.Count == 1);
            Assert.AreEqual("SyncGatewaySession", cookies[0].Name);
        }
        public void TestFacebookAuth()
        {
            var sg = new SyncGateway("http", GetReplicationServer());
            using (var remoteDb = sg.CreateDatabase("facebook")) {
                remoteDb.DisableGuestAccess();
                var doneEvent = new ManualResetEvent(false);

                var accountStore = new ACAccountStore();
                var accountType = accountStore.FindAccountType(ACAccountType.Facebook);

                var options = new AccountStoreOptions();
                options.FacebookAppId = FacebookAppId;
                options.SetPermissions(ACFacebookAudience.Friends, new [] { "email" });

                var success = true;
                ACAccount account = null;
                accountStore.RequestAccess(accountType, options, (result, error) =>
                {
                    success = result;
                    if (success) {
                        var accounts = accountStore.FindAccounts(accountType);
                        account = accounts != null && accounts.Length > 0 ? accounts[0] : null;
                    }
                    else {
                        Log.W(Tag, "Facebook Login needed. Go to Settings > Facebook and login.");
                        Log.E(Tag, "Facebook Request Access Error : " + error);
                    }
                    doneEvent.Set();
                });

                doneEvent.WaitOne(TimeSpan.FromSeconds(30));

                Assert.IsTrue(success);
                Assert.IsNotNull(account);

                var token = account.Credential.OAuthToken;
                Assert.IsNotNull(token);
                Assert.IsTrue(token.Length > 0);

                var url = remoteDb.RemoteUri;

                var cookieStore = new CookieStore(manager.Directory);
                var httpClientFactory = new CouchbaseLiteHttpClientFactory(cookieStore);
                manager.DefaultHttpClientFactory = httpClientFactory;
                Replication replicator = database.CreatePushReplication(url);
                replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(token);

                Assert.IsNotNull(replicator);
                Assert.IsNotNull(replicator.Authenticator);
                Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);

                CreateDocuments(database, 20);

                RunReplication(replicator);

                var urlStr = url.ToString();
                urlStr = urlStr.EndsWith("/") ? urlStr : urlStr + "/";
                //var cookies = httpClientFactory.GetCookieContainer().GetCookies(new Uri(urlStr));
                //Assert.IsTrue(cookies.Count == 1);
                //Assert.AreEqual("SyncGatewaySession", cookies[0].Name);
            }
        }
Exemplo n.º 14
0
        public void TestFacebookAuth()
        {
            var doneEvent = new ManualResetEvent(false);

            var accountStore = new ACAccountStore();
            var accountType  = accountStore.FindAccountType(ACAccountType.Facebook);

            var options = new AccountStoreOptions();

            options.FacebookAppId = FacebookAppId;
            options.SetPermissions(ACFacebookAudience.Friends, new [] { "email" });

            var       success = true;
            ACAccount account = null;

            accountStore.RequestAccess(accountType, options, (result, error) =>
            {
                success = result;
                if (success)
                {
                    var accounts = accountStore.FindAccounts(accountType);
                    account      = accounts != null && accounts.Length > 0 ? accounts[0] : null;
                }
                else
                {
                    Log.W(Tag, "Facebook Login needed. Go to Settings > Facebook and login.");
                    Log.E(Tag, "Facebook Request Access Error : " + error);
                }
                doneEvent.Set();
            });

            doneEvent.WaitOne(TimeSpan.FromSeconds(30));

            Assert.IsTrue(success);
            Assert.IsNotNull(account);

            var token = account.Credential.OAuthToken;

            Assert.IsNotNull(token);
            Assert.IsTrue(token.Length > 0);

            var url = GetReplicationURLWithoutCredentials();

            var cookieStore       = new CookieStore();
            var httpClientFactory = new CouchbaseLiteHttpClientFactory(cookieStore);

            manager.DefaultHttpClientFactory = httpClientFactory;
            Replication replicator = database.CreatePushReplication(url);

            replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(token);

            Assert.IsNotNull(replicator);
            Assert.IsNotNull(replicator.Authenticator);
            Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);

            replicator.Start();

            doneEvent.Reset();
            Task.Factory.StartNew(() =>
            {
                var timeout = DateTime.UtcNow + TimeSpan.FromSeconds(30);
                while (DateTime.UtcNow < timeout)
                {
                    if (!replicator.active)
                    {
                        break;
                    }
                    System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(10));
                }
                doneEvent.Set();
            });
            doneEvent.WaitOne(TimeSpan.FromSeconds(35));

            var urlStr = url.ToString();

            urlStr = urlStr.EndsWith("/") ? urlStr : urlStr + "/";
            var cookies = httpClientFactory.GetCookieContainer().GetCookies(new Uri(urlStr));

            Assert.IsTrue(cookies.Count == 1);
            Assert.AreEqual("SyncGatewaySession", cookies[0].Name);
        }
Exemplo n.º 15
0
		ACAccountStore accountStore; // Save this reference since ACAccounts are only good so long as it's alive

		public override Task<IEnumerable<Account>> GetAccountsAsync ()
		{
			if (accountStore == null) {
				accountStore = new ACAccountStore ();
			}
			var store = new ACAccountStore ();
			var at = store.FindAccountType (ACAccountType.Twitter);

			var tcs = new TaskCompletionSource<IEnumerable<Account>> ();

			store.RequestAccess (at, (granted, error) => {
				if (granted) {
					var accounts = store.FindAccounts (at)
						.Select (a => (Account) new ACAccountWrapper (a, store))
						.ToList ();

					tcs.SetResult (accounts);
				} else {
					tcs.SetResult (new Account [0]);
				}
			});

			return tcs.Task;
		}
Exemplo n.º 16
0
        public void TestFacebookAuth()
        {
            var sg = new SyncGateway("http", GetReplicationServer());

            using (var remoteDb = sg.CreateDatabase("facebook")) {
                remoteDb.DisableGuestAccess();
                var doneEvent = new ManualResetEvent(false);

                var accountStore = new ACAccountStore();
                var accountType  = accountStore.FindAccountType(ACAccountType.Facebook);

                var options = new AccountStoreOptions();
                options.FacebookAppId = FacebookAppId;
                options.SetPermissions(ACFacebookAudience.Friends, new [] { "email" });

                var       success = true;
                ACAccount account = null;
                accountStore.RequestAccess(accountType, options, (result, error) =>
                {
                    success = result;
                    if (success)
                    {
                        var accounts = accountStore.FindAccounts(accountType);
                        account      = accounts != null && accounts.Length > 0 ? accounts[0] : null;
                    }
                    else
                    {
                        Log.W(Tag, "Facebook Login needed. Go to Settings > Facebook and login.");
                        Log.E(Tag, "Facebook Request Access Error : " + error);
                    }
                    doneEvent.Set();
                });

                doneEvent.WaitOne(TimeSpan.FromSeconds(30));

                Assert.IsTrue(success);
                Assert.IsNotNull(account);

                var token = account.Credential.OAuthToken;
                Assert.IsNotNull(token);
                Assert.IsTrue(token.Length > 0);

                var url = remoteDb.RemoteUri;

                var cookieStore       = new CookieStore(manager.Directory);
                var httpClientFactory = new CouchbaseLiteHttpClientFactory(cookieStore);
                manager.DefaultHttpClientFactory = httpClientFactory;
                Replication replicator = database.CreatePushReplication(url);
                replicator.Authenticator = AuthenticatorFactory.CreateFacebookAuthenticator(token);

                Assert.IsNotNull(replicator);
                Assert.IsNotNull(replicator.Authenticator);
                Assert.IsTrue(replicator.Authenticator is TokenAuthenticator);

                CreateDocuments(database, 20);

                RunReplication(replicator);

                var urlStr = url.ToString();
                urlStr = urlStr.EndsWith("/") ? urlStr : urlStr + "/";
                //var cookies = httpClientFactory.GetCookieContainer().GetCookies(new Uri(urlStr));
                //Assert.IsTrue(cookies.Count == 1);
                //Assert.AreEqual("SyncGatewaySession", cookies[0].Name);
            }
        }