简体   繁体   中英

Facebook Graph API /me Request

I am attempting to incorporate a Facebook login into my Android Application. The issue that I am having is that I cannot seem to make a successful /me request request with extra parameters.

If I attempt to do a /me request on its own, than I can get the User ID, and Name. I want to get extra fields, such as email, first_name, last_name, etc.

ERROR

{Response: responseCode: 400, graphObject: null, error: {HttpStatus: 400, errorCode: 2500, errorType: OAuthException, errorMessage: An active access token must be used to query information about the current user.}}

TRYING TO MAKE REQUEST

I do this from within the onSuccess method inside a LoginManager.

new GraphRequest(
    loginResult.getAccessToken(),
    "/me?fields=id,name,email",
    null,
    HttpMethod.GET,
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* handle the result */
        }
    }
).executeAsync();

I think something is wrong at loginResult.getAccessToken(), try this:

    Bundle params = new Bundle();
    params.putString("fields", "id,name,email");
    new GraphRequest(
            AccessToken.getCurrentAccessToken(), //loginResult.getAccessToken(),
            "/me",
            params,
            HttpMethod.GET,
            new GraphRequest.Callback() {
                public void onCompleted(GraphResponse response) {
                    try {
                        Log.e("JSON",response.toString());
                        JSONObject data = response.getJSONObject();
                        //data.getString("id"),
                        //data.getString("name"),
                        //data.getString("email")
                    } catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
    ).executeAsync();

Check login success with full Permission you want

if (AccessToken.getCurrentAccessToken().getDeclinedPermissions().size() == 0) {

}

I can try to help you out since I just went through the facebook log in and pull back name and email but we really need to see all your code. I'd post all that you can that has anything to do with how you are using facebook classes. Here are of what I used if it helps at all: 内容(如果有帮助的话):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    loginButtonFacebook = (LoginButton) findViewById(R.id.login_button_facebook);
    loginButtonFacebook.setReadPermissions("user_friends,email");

    LoginManager.getInstance().registerCallback(callbackManager,
            new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    accessToken = loginResult.getAccessToken();

                    final SharedPreferences.Editor editor = prefs.edit();
                    editor.putString("facebookAccessToken", accessToken.getToken()).apply();
                    GraphRequest request = GraphRequest.newMeRequest(
                            accessToken,
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject object, GraphResponse response) {
                                    getFacebookItems(object);
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,first_name,last_name,email,picture");
                    request.setParameters(parameters);
                    request.executeAsync();
                }

                @Override
                public void onCancel() {
                    Log.d(TAG, "Canceled");
                }

                @Override
                public void onError(FacebookException exception) {
                    Log.d(TAG, String.format("Error: %s", exception.toString()));
                }
            });
}
....


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);

}


public void getFacebookItems(JSONObject object){
    try {
        facebookId = object.getString("id");
        facebookName = object.getString("name");
        facebookFirstName = object.getString("first_name");
        facebookLastName = object.getString("last_name");
        facebookEmail = object.getString("email");

    }catch (JSONException e) {
        e.printStackTrace();
    }
}

I also access facebook user friends from another activity so I store my accesstoken in preferences for when I need it later.

    accessTokenString = prefs.getString("facebookAccessToken", "");
    String facebookId = prefs.getString("facebookId", "");
    if (facebookId.length() > 0 && accessTokenString.length() > 0) {
        accessToken = new AccessToken(accessTokenString, getString(R.string.facebook_app_id), facebookId, null, null, null, null, null);
        storeFacebookFriends();
    }

....

public void storeFacebookFriends(){

    if (accessToken != null) {
        GraphRequestBatch batch = new GraphRequestBatch(

            GraphRequest.newMyFriendsRequest(
                    accessToken,
                    new GraphRequest.GraphJSONArrayCallback() {
                        @Override
                        public void onCompleted(JSONArray jsonArray, GraphResponse response) {
                            // Application code for users friends
                            userArray = jsonArray;
                        }
                    })
        );
        batch.addCallback(new GraphRequestBatch.Callback() {
            @Override
            public void onBatchCompleted(GraphRequestBatch graphRequests) {
                // Application code for when the batch finishes
            }
        });
        batch.executeAsync();

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM