Xamarin Android – Twitter authentication

Xamarin Android – Twitter authentication

TwitterXamarin

Short introduction

Nowadays many mobile applications (but not only) offer login with standard credentials (user, password) or with social media services like Facebook, Twitter or LinkedIn.

In this post I will show how easily you can add Twitter authentication to Xamarin Android app because there is less tutorials about this topic.

Twitter integration with Xamarin Android

What do I need to start?

1) Visual Studio 2013 Community (for free) or higher

2) Xamarin Android subscription (you can test it for free here)

3) Android Phone or Xamarin Android Player Emulator (download here)

4) Twitter account

 

Twitter integration with Xamarin Android

1) Firstly we must create Twitter application if we want to integrate it in mobile application. Type this url in web browser and click sign in:

twitterAuth1

2) Sign in with your normal Twitter account credentials:

twitterAuth2

3) Click “Create new app”:

twitterAuth3

4) Type the name of your application (in my case it is “TwitterXamarinApp”). You must also provide description of your app and website (if you don’t have one, you can type the address of my blog). Last thing is to check “Yes I agree” at the end:

twitterAuth4

5) Congratulations! You have created Twitter application. Now we must configure it so we can use it in Xamarin Android app:

twitterAuth5

6) Go to “Settings” Type http://mobile.twitter.com in “Callback” URL field. Remember also that checkbox “Allow this application to be used to Sign In with Twitter” must be checked. Click “Update settings” at the bottom:

twitterAuth6

twitterAuth7

7) The last point is connected with permissions. Go to “Permissions” and select “Read, Write and Access direct messages”. After that click “update settings”:

twitterAuth8

8) After above configuration click “Test Auth”. This one will check if Twitter app is correctly configured. “Get auth signature below” is also for testing connection (you don’t have to click it):

twitterAuth9

twitterAuth10

Great, your Twitter application is ready. Now it is time to connect this app with our Xamarin Android app.

Now we must create Xamarin Android app and add authentication with Twitter to it.

Do not close this page (we will need it).

 

1) Open Visual Studio and create new Xamarin Android project like shown in the screenshots below (in my case it is “SocialMediaApp”):

twitterAuth11

twitterAuth12

twitterAuth13

twitterAuth14

2) Now we must add Xamarin Component to our app – “Xamarin.Auth”. This component will help us with adding authentication with Twitter (or other social services):

twitterAuth15

twitterAuth16

twitterAuth17

twitterAuth18

twitterAuth19

3) Now open file “Main.axml” in designer and paste whole below code:

 

<!--?xml version=”1.0″ encoding=”utf-8″?-->  
<linearlayout xmlns:android="”http://schemas.android.com/apk/res/android”" android:orientation="”vertical”" android:layout_width="”fill_parent”" android:layout_height="”fill_parent”">  
<button android:id="”@+id/TwitterButton”" android:layout_width="”fill_parent”" android:layout_height="”wrap_content”" android:text="”Twitter”" android:background="”#55ACEE”">  
</button></linearlayout>  

 

Activity should looks like below screenshot:

twitterAuth20

4) Open MainActivity class:

a) Change the code in “OnCreate” method:

protected override void OnCreate(Bundle bundle)  
{  
base.OnCreate(bundle);  
SetContentView(Resource.Layout.Main);  
Button button = FindViewById<button>(Resource.Id.TwitterButton);  
button.Click += delegate { LoginTwitter(); };  
}  
</button>  

 

b) After closing mark of OnCreate method add two additional fragments:

private void LoginTwitter()  
 {  
   var auth = new OAuth1Authenticator(  
   consumerKey: “YOUR_KEY”,  
   consumerSecret: “YOUR_SECRET”,  
   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://mobile.twitter.com”)  
   );  
  
 auth.Completed += twitter_auth_Completed;  
 StartActivity(auth.GetUI(this));  
 }  
  
private void twitter_auth_Completed(object sender, AuthenticatorCompletedEventArgs eventArgs)  
 {  
  if (eventArgs.IsAuthenticated)  
   {  
    Toast.MakeText(this, “Authenticated!”, ToastLength.Long).Show();  
   }  
 }  

 

c) Add using statement like on the screenshot below:

twitterAuth21

5) Open Twitter page (the same like previously). Go to “Details” and then click “manage keys and access tokens):

twitterAuth22

6) Copy “Consumer Key” and “Consumer Secret” values and paste them in the “TwitterLogin()” method like below:

twitterAuth23

twitterAuth24

7) Now start Xamarin Android player (or plugin your Android phone) and launch application:

twitterAuth25

twitterAuth26

8) Press “Twitter” button. As you can see below you will be redirect to Twitter login page. If you type correct credentials to your account, you will be authenticated:

twitterAuth27

twitterAuth28

twitterAuth29

Sum up

In this postI tried to present how to integrate Xamarin Android application with Twitter so user can login with Twitter account.

Enjoy! 🙂

Advertisements