找到你要的答案

Q:Facebook Graph API /me Request

Q:脸谱网图形API / ME请求

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();

我试图把一个脸谱网登录到我的Android应用程序。我的问题是,我似乎不能作出一个成功的/我的请求请求额外的参数。

如果我试图做一个/我自己的要求,比我可以得到用户ID和名称。我想得到额外的字段,如电子邮件,first_name,last_name,等。

误差

{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.}}

试图提出请求

我做这里面的会在方法内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();
answer1: 回答1:

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) {

}

我认为是错误的东西在loginresult getaccesstoken(),试试这个:

    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();

检查登录成功与您想要的全部权限

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

}
answer2: 回答2:

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 parts 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();

    }
}

我可以尝试帮助你,因为我刚刚经历了脸谱网登录和拉回来的名称和电子邮件,但我们真的需要看到你所有的代码。我将张贴所有你可以与你使用脸谱网类有任何关系。这里是我使用的部分,如果它有助于所有:

@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();
    }
}

我也访问脸谱网的用户朋友从另一个活动,我把我的accesstoken喜好当我需要它以后。

    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();

    }
}
android  facebook  facebook-graph-api