segunda-feira, 7 de novembro de 2011

Integrating Facebook and Twitter in to your Android app

Today I'll show the way to integrate Facebook and Twitter in to your Android app!

Don't know what Android is? Wanna get started? Follow this link:
Getting started on Android!

So you have a game and you have that one great idea to integrate in to the social networks that will really launch it up and it'll be great for marketing? Or maybe a new gameplay mechanic that uses those? Tell me about them, because I can't really think of any great uses.

Or if you don't wanna make a game per see, but you have THE idea for an application that uses those networks that will net you endless money? Well, let's get going then.

Lucky for you, both Facebook and Twitter are really easy to integrate because most of the work is already done in the internet, especially on Facebook. Twitter is a little bit more tricky to find the right resources, but I've done just that for you, dear reader. For Facebook I'm going to give you a step-by-step in to using the SDK,

Skip to the end of the post if you just want some samples :D

Integrating to Facebook

  1. Create an APP in your account
  2. Download the Facebook SDK for Android and put it on Eclipse as a library project
  3. Import that in to your Android application 
  4. Get to know the SDK by messing with the samples!
The Facebook logo. Pretty, huh?
Create an APP in your account
For Facebook, you're gonna need an account. go here and you'll be prompted to login. In that screen you can create an application, after you've sign up to be a developer (this can be tricky, since you need to authenticate with a cellphone and stuff). That application is sort of like, the point of access your Android application will use to access the Facebook API. Keep this page open, you're gonna need to come back to it, to get the API Key.

Download the Facebook SDK for Android and put it on Eclipse as library project go here and click on the ZIP button to download everything as a single ZIP file. Inside the ZIP, look for a "facebook" folder. That has the basic project that you need to access the API.
Then, you're gonna need to create a new Android application(any name and Android version will do) and you have to copy the contents of the "facebook" folder in to the folder your new application, replacing the files. At Eclipse, press F5 in your project to refresh and show the new changes. Then, right-click in your project and go to properties. Go to Android and check the checkbox "is a library", if it isn't already checked. You should have something like this(doesn't have to be the exact same):

It doesn't have to look exactly like this, but you get the idea
Import that in to your Android application
Right click on the project you wanna import it in to, properties, Android, in the Library lower part, click on Add. Pick the Facebook project and hit ok. Now play around with the classes!

Get to know the SDK by messing with the samples!
In the ZIP you downloaded with the SDK, there are also samples. Just create new Android projects and copy-paste the samples codes in to the projects to try them out on the emulator. Don't forget to examine the code to switch up the API Key in the code with the API key of your facebook application! Happy programming :)

Integrating to Twitter

You're going to need to be an Twitter developer, (you're gonna need an twitter account too) go here and hit sign in, in the upper right screen. Afterwards, click "Create an APP", fill the fields, hit ok, go. Then, you need to get the Consumer Key and the Consumer Secret of your twitter application, since you'll have to feed those in to your Android App.

The twitter application you create in the website is more like a point of access for your Android App

Then you need to code the Android part:
Ok, this is how I did it... I downloaded dewaele's sample twitter project and it worked! this is his profile on gitHub and this is the twitter sample.

Download it and import it in to Eclipse (with file => import => import existing projects in to workspace => Pick the folder with the sample)
This already has all the necessary libraries, easily configured. The whole OAUTH is kinda hard to code by yourself, so it's best to use this sample. Fill in the Consumer key and secret in to the Activity class. Happy programming :)

Some words on OAUTH
This is basically Open Authorization. It is a way to access a site's content without exposing password, it is used for security purposes. If you're looking in to using webservices and things like that in your applications, you would best learn a lot of it, since this is what you'll need to implement in order to integrate with any site that uses them(both twitter and facebook use them, naturally), that way you'll be able to integrate technologies that haven't been integrated before, giving you a lot of power :)

Final words
So basically you can now use Facebook and Twitter in your applications. If the above text is too complicated, just follow these links of ddewale (he rocks):
But don't forget you need to be an developer in each site with an APP registered there :D

Woosh, I wrote this because I actually have do to these things for a game in a college assignment... I'll share that one with you in another opportunity :)

Comment if you run in to trouble... Go program now!

7 comentários:

  1. Mabiremps, deu erro quando fui testar o projeto do Twitter, na onAuth, nao aconteceu isso contigo nao?

  2. Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!

    Android Application Development

  3. There are multiple ways of doing the integration. One is to make use of the SDKs available from Facebook, Twitter etc. Second is to make use of open an embedded browser control for authentication and finally the REST APIs provided to post the update. Both of these are slightly tedious, since either you need to download and use the different APIs or you need to implement the entire protocol.

  4. Error: MyApp in misconfigured for Facebook login. Prees Okay to go back to the application without connecting it to Facebook.

  5. Thanks it is very useful

  6. Useful things, but similar example with video sharing over twitter will be much more useful.