ulmaxy
ulmaxy

Reputation: 870

Facebook Android SDK: Get user by app-scoped userId

I've been working with facebook android sdk and trying to implement the following scheme:

  1. My android app asks user for access to his facebook account
  2. I receive the user access token as a login result
  3. I pass that token (which is a simple String) to my own backend server
  4. On my server I need to find out who that user is and if there's an account on my backend server, that is connected to this user's facebook account, let the user login, otherwise show error message.

My problem is that from access token I can only retreive app-scoped userId, which can't be used by my backend to access the user information from facebook server. The code I use for handling login result is pretty simple:

facebookButton.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
        override fun onSuccess(result: LoginResult?) {
            //Pass token to my backend
            loginByToken(result?.accessToken?.token!!, FACEBOOK_PROVIDER)
        }

        override fun onCancel() {
            Toast.makeText(context, "Facebook login cancelled", Toast.LENGTH_SHORT).show()
        }

        override fun onError(error: FacebookException?) {
            Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show()
        }
    })

I'm also using the following autorization scheme: enter image description here

So, my question is how do I get the user data on my backend server? I guess it'd make sense if I also provide my app id for request user data, but I went through facebook documentation, and haven't found any way of doing so

Upvotes: 0

Views: 62

Answers (1)

andyrandy
andyrandy

Reputation: 74014

The only way to get data of the user is to use his access token. Since you pass that token to your server anyway, it should be no problem to use it:

/me?fields=id,name,...&access_token=xxx

For identifying returning users, you only need the app scoped id though. If you want to identify users across different Apps: https://developers.facebook.com/docs/apps/for-business/

Upvotes: 1

Related Questions