Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use facebook login to access data from backend server in Android app?

I am working on an app in which I have to use facebook login for accessing data from my backend server. I have search on this and got that:

  1. First, the user will enter username and password of facebook then the request goes to server.
  2. If user authenticated then fetch access token of user.
  3. Send this access token on server.
  4. The server will verify this access token.

I have successfully connected my app with facebook i.e, now user can logged from my app to facebook. But I don't now how can I get the access token of user and also how I can verify this access token on server.

Can you provide me some sample code for this. Please help me I am stuck in it from a long time.

like image 313
Sagar Trehan Avatar asked Mar 19 '13 10:03

Sagar Trehan


1 Answers

I'm not sure if it is best practice, but this is how I do it.

When you log a user in Android on the client device via any of the SDKs, you get an user access token. The token can be accessed as follows

AccessToken token = AccessToken.getCurrentAccessToken();
if (token != null) {
  Toast.makeText(getActivity(), token, Toast.LENGTH_LONG).show();
}

You can then pass this token to the backend as a POST variable:

'access_token': '32b409xceBV78d2932b409xceBV78d29'

Using a backend SDK, you can get the user info again. Here's a example in python

facebook_graph = facebook.GraphAPI(access_token)

If the email and fbid match the user information stored in your database, you grant access:

user = get_user(email=facebook_graph['email'], fbid=facebook_graph['fbid'])
login(request, user)
like image 200
keithhackbarth Avatar answered Oct 18 '22 19:10

keithhackbarth