javafacebookfacebook-graph-apioltu

Retrieve email information of facebook account


When I get the facebook code, I set the scope in this way:

OAuthClientRequest request = OAuthClientRequest
    .authorizationProvider(OAuthProviderType.FACEBOOK)
    .setClientId(FacebookConfig.APP_ID)
    .setRedirectURI(FacebookConfig.APP_REDIRECT_URI)
    .setScope("email")
    .buildQueryMessage();

Now, in the app info on facebook account, I can see that the info provided to the app contains the email address.

enter image description here

But when I retrieve the infos:

OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());  
OAuthClientRequest bearerClientRequest =new OAuthBearerClientRequest("https://graph.facebook.com/me")
                                            .setAccessToken(accessToken)
                                            .buildQueryMessage();

OAuthResourceResponse resourceResponse=oAuthClient.resource(bearerClientRequest, OAuth.HttpMethod.GET, OAuthResourceResponse.class);

there is any information about the email address:

{"name":"AAAAA BBBBB","id":"12345678901234567"}

What's wrong?
Thanks.


Solution

  • If you're using the Graph API v2.4, you need to specify each field which you want to retrieve explicitly.

    E.g.

    OAuthClientRequest bearerClientRequest =new OAuthBearerClientRequest("https://graph.facebook.com/me?fields=id,name,email")
                                                .setAccessToken(accessToken)
                                                .buildQueryMessage();