Gestulator

10.6.11

Integrating Twitter in Windows Phone App using TweetSharp

In this post we'll discuss how to integrate Twitter in your Windows Phone Silverlight App and make it more social.

I'll be discussing the Authorization part and then we'll send a Tweet.

I'll be using the Tweetsharp assemblies which is available here. It supports Windows Phone 7, and will probably support Mango too, but keep an eye there.

If you don't know how OAuth for Twitter works, visit apiwiki.twitter.com. You will also require Consumer Key and Consumer Secret from dev.twitter.com for your App, sign in there with your Twitter account, register an desktop client app and you'll get the Consumer Key and Consumer Secret.


Now following will be the OAuth workflow of App using Twitter:

1. Get OAuth Request token.
2. Get the Authorization URL and redirect user to web browser and allow User to grant access.
3. Get back the 7 digit code via User.
4. Get OAuth Acces token using Request token and the 7 digit code.
5. Authenticate Twitter Service with Accest token.
Done!



Declaration:

Add all the four Tweetsharp dlls as reference in your Project.

We'll be using TweetSharp so add:

using TweetSharp;

And now declare the Twitter Service with Consumer Key and Secret and Request, Access Token for Authorization:

TwitterService Twitter = new TwitterService("YourConsumerKey","YourConsumerSecret");
OAuthRequestToken Request = new OAuthRequestToken();
OAuthAccessToken Access = new OAuthAccessToken();


Getting Request token and Authorization URL:

Now to get Request Token and get the Authorization URL, add:

Twitter.GetRequestToken((request, Response) =>
{
    if (Response.StatusCode == HttpStatusCode.OK)
    {
        Request = request; // Store it for later use
        Uri uri = Twitter.GetAuthorizationUri(request);
       
        WebBrowserTask Web = new WebBrowserTask();
        Web.URL = uri.ToString();
        Dispatcher.BeginInvoke(() => Web.Show());
    }
});

To Redirect the user to Grant access to App, I have used WebBrowserTask instead of WebBrowser control which becomes messy, so add:

using Microsoft.Phone.Tasks;

 
Getting Access token and final Authentication:

Now we have the 7 digit code from user and the Request token, we use following code to Authenticate:

Twitter.GetAccessToken(Request, txtCode.Text, (access,Response) =>
{
    if (Response.StatusCode == HttpStatusCode.OK)
    {
        Access = access; // Store it for reuse

        Twitter.AuthenticateWith(access.Token, access.TokenSecret);
    }
});

Now you will have to save the Access token or the Token and TokenSecret String to Islates Storage Settings or Isolated Storage. The next time you will skip to Authentication.


Sending Tweet! :

Use the following code to send a Tweet:

Twitter.SendTweet(txtTweet.Text, (Status, Response) =>
{
    if (Response.StatusCode == HttpStatusCode.OK)
    {
        MessageBox.Show("Tweet Sent !");
    }
});

To access other Twitter Features, visit the documentation of TweetSharp here.

Finally check out my Windows Phone Apps using this code for Twitter Integration here.

If you have any queries, leave a comment or contact me at @Nirmitk26.

5 comments:

  1. Hi Nirmit, you say "The next time you will skip to Authentication".
    Then if I have Client key and secret and token key and secret i can use:
    TwitterService Twitter = new TwitterService("YourConsumerKey","YourConsumerSecret", "tokenKey", "TokenSecret");
    and
    Twitter.SendTweet(txtTweet.Text, (Status, Response) =>
    {
    if (Response.StatusCode == HttpStatusCode.OK)
    {
    MessageBox.Show("Tweet Sent !");
    }
    });


    ?

    Thenks

    ReplyDelete
  2. i can't extract the project it return error

    ReplyDelete
  3. could you please upload it again

    ReplyDelete
  4. hi...Im student from Informatics engineering nice article,
    thanks for sharing :)

    ReplyDelete
  5. Hi.. I can not find the source code to download. Can you provide the source code for twitter integration?? Thanks in advance..

    ReplyDelete