7

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.

Ariel Magbanua
  • 3,083
  • 7
  • 37
  • 48
Sagar Trehan
  • 2,401
  • 2
  • 24
  • 32
  • 1
    Refer to following blog for details discussion: https://library.launchkit.io/the-right-way-to-implement-facebook-login-in-a-mobile-app-57e2eca3648b#.rc1d91uue. Its very helpful – Sagar Trehan Feb 05 '16 at 06:13
  • same question at http://stackoverflow.com/questions/12065492/rest-api-for-website-which-uses-facebook-for-authentication but has good anwers – ob.yann Feb 22 '17 at 02:24

2 Answers2

1

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)
keithhackbarth
  • 9,317
  • 5
  • 28
  • 33
0

To access the data of the facebook user you have to take permissions and the following link may be useful for you. Just check it out... https://developers.facebook.com/docs/howtos/androidsdk/3.0/fetch-user-data/

Abhishek Agarwal
  • 1,907
  • 12
  • 21
  • Abhishek I had already go through it, but I want to know that how I can verify the user on my server end using facebook login – Sagar Trehan Mar 19 '13 at 12:08