Tags

, , , , , , , , , ,

Twitter’s xAuth is an exciting option which is not available for web based applications but available for desktop and mobile apps that uses Twitter SDK where standard web OAuth flow or PIN-code out-of-band flow is not an option. xAuth access is restricted to approved applications and if your present/past/future app needs it, request if from here https://support.twitter.com/forms/platform. I was using this xAuth in 2012 when I was doing a good solution for blind people for Microsoft Imaginecup contest.

Ok, lets back to the topic, for Xamarin, we have Twitter4j component which is such awesome and very simple component to provide Twitter SDK capabilities. Once you add that component it open its official documentation, that is a small code implementation with access to twitter using a previously created twitter app’s Consumer Key, Consumer Secret, AccessToken and AccessToken Secret. Today I tried to port this code to use with an xAuth enabled app. The code is,

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Threading;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

using Twitter4j;
using Twitter4j.Auth;
using Twitter4j.Conf;

namespace TwitterXAuthTestAndroid
{
  [Activity (Label = "TwitterXAuthTestAndroid", MainLauncher = true, Icon = "@drawable/icon")]
  public class MainActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
      base.OnCreate (bundle);

      SetContentView (Resource.Layout.Main);

      // Get our button from the layout resource,
      // and attach an event to it
      Button button = FindViewById<Button> (Resource.Id.myButton);

      ConfigurationBuilder TwitterConfig = new ConfigurationBuilder();
      TwitterConfig.SetOAuthConsumerKey("_my_app's_consumer_key_");
      TwitterConfig.SetOAuthConsumerSecret("_my_app's_consumer_secret);

      IConfiguration TwitterICnfg = TwitterConfig.Build ();

      TwitterFactory factory = new TwitterFactory (TwitterICnfg);

      OAuthAuthorization auth = new OAuthAuthorization (TwitterICnfg);

      AccessToken acctkn = auth.GetOAuthAccessToken ("_my_twitter_user_name_eg_(isham_m_iqbal)", "_my_password_");
      ITwitter twitter = factory.GetInstance(acctkn);

      button.Click += delegate {
        ThreadPool.QueueUserWorkItem(state =>
          {
            try
            {
              twitter.UpdateStatus("Isham_M_Iqbal rocks.. boom....");
            }

            catch(Exception ed)
            {
              Console.WriteLine(ed.Message);
            }
          }
        );
      };
    }
  }
}

Lines 30-32 makes a twitter configuration with my twitter app’s ConsumerKey and ConsumerSecret. After it in line 34 builds the above configuration and pass it to IConfiguration interface. Line 40 is the place, that does the xAuth magic. Refer this code, if you get “The screen name / password combination seems to be invalid.” error, that mean’s your username-password combination is wrong or xAuth wasn’t enabled for your twitter app. I hope you enjoy this post.

Happy coding peeps 🙂

Advertisements