1

I have a problem with implementing twitter login in my Xamarin Android. I have included Xamarin.Auth component and it works fine for Facebook. Fot twitter auth.Completed event is not called... I have created sample app on twitter dev portal.

Here is my code from the app:

private void LoginTwitter()
    {
        var auth = new OAuth1Authenticator(
                  consumerKey: "3v7rOXkdexGYhQmr3HVhtGgPO",
                  consumerSecret: "mGhRjee87tAp4X0vHUmMIohWoYy0JGg9zFGyin7CigFP64y3j5",
                  requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"),
                  authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"),
                  accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"),
                  callbackUrl: new Uri("http://twitter.com")
              );
        auth.AllowCancel = true;
        StartActivity(auth.GetUI(this));
        auth.Completed += (s, eventArgs) =>
        {
            if (eventArgs.IsAuthenticated)
            {

                Account loggedInAccount = eventArgs.Account;
                //save the account data for a later session, according to Twitter docs, this doesn't expire
                AccountStore.Create(this).Save(loggedInAccount, "Twitter");
            }
        };
    }

I hope someone will help.

Daniel Krzyczkowski
  • 2,732
  • 2
  • 20
  • 30

2 Answers2

7

Ok, I have solved the problem by myself. When creating new OAuth1Authenticator CallBack Url should be set to mobile.twitter.com not twitter.com

callbackUrl: new Uri("http://mobile.twitter.com")

After that you will be able to obtain token.

Hope it will help someone in the future. :)

EDIT: You need to use http://mobile.twitter.com/home now-

Tóth Tibor
  • 1,526
  • 12
  • 21
Daniel Krzyczkowski
  • 2,732
  • 2
  • 20
  • 30
0

I think you need to move your Completed event handling up as I believe StartActivity is blocking.

private void LoginTwitter()
{
    var auth = new OAuth1Authenticator(
              consumerKey: "3v7rOXkdexGYhQmr3HVhtGgPO",
              consumerSecret: "mGhRjee87tAp4X0vHUmMIohWoYy0JGg9zFGyin7CigFP64y3j5",
              requestTokenUrl: new Uri("https://api.twitter.com/oauth/request_token"),
              authorizeUrl: new Uri("https://api.twitter.com/oauth/authorize"),
              accessTokenUrl: new Uri("https://api.twitter.com/oauth/access_token"),
              callbackUrl: new Uri("http://twitter.com")
          );
    auth.AllowCancel = true;
    auth.Completed += (s, eventArgs) =>
    {
        if (eventArgs.IsAuthenticated)
        {

            Account loggedInAccount = eventArgs.Account;
            //save the account data for a later session, according to Twitter docs, this doesn't expire
            AccountStore.Create(this).Save(loggedInAccount, "Twitter");
        }
    };

    StartActivity(auth.GetUI(this));
}
Cheesebaron
  • 24,131
  • 15
  • 66
  • 118