Pages

Saturday, May 25, 2013

Getting facebook user access token in Android


For one of my semester projects, I had to develop an android application which would allow users to login to facebook and then let the application obtain the user access token from the session object. This user access token can be used to obtain/download all the data about you and your friends which  you can access normally through browser. In my case, I had to download profile picture albums of logged in user's friends, so I generated an access token based on my requirements.

I would advise you to go through one step each here and before working on it, read it completely. Sometimes, I can give some troubleshooting before the end of the step which might save some of your time.

Starting from scratch, you have to do following things:

Step 1:

Open Getting Started Guide with the Facebook SDK for Android.

Follow Step-1, Step-2, Step-3.  You don't need to do Step-4 (Run the samples).

When you add link Facebook SDK, you might have to remove 'android-support-v4.jar' from lib folder of your project. It was showing some error because there were two versions (libs + Facebook SDK)

Step 2:

2.1) Follow the step-5 from the link. You can generate a facebook app from here and when it asks to generate a hash key, you can use following command on command prompt on Windows.

>  keytool -exportcert -alias androiddebugkey -keystore %HOMEPATH%\.android\debug.keystore | openssl sha1 -binary | openssl base64

It will ask for password which is "android" without quotes.

2.2) With the created app, go to Basic Settings, click on checkbox saying (Native Android App) and provide the information as given in the sample tutorial.

2.3) You have to deselect sandbox option in the app settings which otherwise restricts app's access to developers only. This is very important if you want other users to use your application.

2.4) You can play with Graph API Explorer to see what kind of permissions you need for your application. And you choose all the permissions for now which you can change later from here again.

3) Follow step-6 as given. You can skip adding code to the MainActivity.java for now.

4)  Copy MainActivity class code from here given in update-1.

In onResume() method, we are printing access token which we can use to access any kind of data permissible by access token. I used this token to download profile pictures which I will explain in another post.


No comments:

Post a Comment