Scroll Top

Xamarin Mobile Apps Authentication using Xamarin.Auth – Twitter

Overview

This article is a continuation of the article on the topic Xamarin.Auth Mobile Apps Authentication With Facebook. In this article, we will cover how to authenticate users with one of the most popular social accounts called Twitter.

If you have not read the previous series you can always read them by clicking on below links:

Registering on Twitter Developer portal

As a first step, we should register an app in the Twitter Developer Console. And, that can be achieved with the following steps:

2-createapp

  • Fill the required fields in Application Details (Give the website as any valid Uri and give Callback URL as http://mobile.twitter.com). And then click on create a Twitter Application.

3-details

  • Thus we have integrated our app with Twitter and we can find our API Key and secrets by clicking on Keys and Access Tokens.
  • And we should create an access token in order to make API calls to get the requested user data from the server, which can be done by clicking on create my access token button at the bottom of the window as shown below.  

4-keys-and-values

  • Now we can see Api Key and Api Secret values in the keys and access token section which is shown in the above step. And we can also find Authorize URL, Request token URL, Callback URL and Access Token URL by clicking on Details section as shown below.

5-values

  • Great! Our app is successfully integrated with a Twitter developer account. Now we can use the above parameters to authenticate the users with Twitter.

Gathering user information from Twitter

Let us make use of those parameters in our application and implement the following code in MainActivity.cs:

  • Add below line of code to LinearLayout inside the Mail.axml to implement the button for Twitter.

  • Add below code to find a button from the view by using the Id attribute and assign it to the variable inside OnCreate().

  • The application uses the OAuth1Authenticator and OAuth1Request classes, provided by Xamarin.Auth, to implement the application side of the authentication flow with Twitter.
  • Implement an authentication inside the click event handler for the button.

  • With the above code in place, we can authenticate the user with the Twitter account and we can also get their user information.

OAuth1Authenticator

Unlike Gmail and Facebook, where we primarily used oAuth2Authenticator, for registering the user with Twitter we will use oAuth1Authenticator. Twitter provides us with two different unique parameters such as Key and Secret for identifying the user,  whereas oAuth2Authenticator provides only one parameter i.e. ClientId.

And, it takes different parameters for authenticating the user, as listed below:.

Parameter Name Explanation A value used in Above Code
consumerKey This identifies the client that is making the request. Give Twitter key here
consumerSecret This identifies the client with this unique secret value and we can find this value in keys and Access Tokens section Give Twitter secret here
requestTokenUrl We can find this value in details section in the developer console. (Reserved) new Uri(TWITTER_REQTOKEN_URL)
authorizeUrl We can find this value in details section in the developer console. (Reserved) new Uri(TWITTER_AUTH)
accessTokenUrl We can find this value in details section in the developer console. (Reserved) new Uri(TWITTER_ACCESSTOKEN_URL)
callbackUrl This is nothing but a redirect Uri(We saw in oAuth2) which helps us to navigate to our app after successful authentication. We can find this value in details section  in the developer console. new Uri(TWITTER_CALLBACK_URL)

oAuth1Request

Once the user authenticated successfully by using oAuth1Authenticator class with the Twitter login page, the application requests for accessing the user information and this request is made with OAuth1Request.

And it takes the following parameters for requesting the user information.

Parameter Name Explanation Value used in Above Code
method It is used for get the required parameters from authorization service. GET
url It is the Graph Uri and it will request the required parameters through this url. new Uri(TWITTER_GRAPH_URL)
parameters Reserved null
account The account which we are authenticating. e.Account

oAuth1 and oAuth2 Difference

We have couple of extra steps when we authenticate using oAuth1 in comparison with oAuth2. We can find the difference between oAuth1 and oAuth2 as shown in the below picture.

6-oauth1vsoauth2

Summary

In this article, we have gone through various steps involved in authenticating users with Twitter account by using Xamarin.Auth component.

In the whole series of articles, we’ve learned how to make use of Xamarin.Auth Component to avail registration facility by linking to social accounts such as Gmail, Facebook using OAuth2Authenticator and Twitter using OAuth1Authenticator. We have also understood how to make use of caching mechanism ( storing user details ), so that users will login only once into the application (using their social account details such as facebook, gmail and twitter). Subsequent logins will be authenticated using cached user details. And Xamarin.Auth also supports some more platforms like GitHub, LinkedIn and Foursquare etc.,

At Walking Tree, we help you take advantage of Xamarin to build amazing enterprise-level mobile applications. We will be happy to assist you to leverage this awesome framework!

References

Leave a comment

Privacy Preferences
When you visit our website, it may store information through your browser from specific services, usually in form of cookies. Here you can change your privacy preferences. Please note that blocking some types of cookies may impact your experience on our website and the services we offer.