Android AccessToken.getCurrentAccessToken()在另一个活动中始终为null

时间:2016-01-28 13:36:14

标签: android facebook facebook-graph-api token

我正在尝试实施一项显示该用户的Facebook好友的活动。

GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {
                if (response.getError() != null)
                    Log.d("XXX", "Facebook graph request error");
                else
                    Log.d("XXX", "Facebook graph request success");
            }
        });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "friends");
        request.setParameters(parameters);
        request.executeAsync();

但实际上currentAccessToken始终为null。即使用户刚刚在10秒前登录。

这是我的登录代码:

ParseFacebookUtils.logInWithReadPermissionsInBackground(this, Arrays.asList("public_profile", "user_birthday", "user_friends", "user_photos", "email"), new LogInCallback() {
        @Override
        public void done(ParseUser user, ParseException err) {
            if (user == null) {
                Log.d("XXX", "Uh oh. The user cancelled the Facebook login.");
            } else if (user.isNew()) {
                Log.d("XXX", "User signed up and logged in through Facebook!");
                MakeMeRequest();
            } else {
                Log.d("XXX", "User logged in through Facebook!");
                MakeMeRequest();
            }
        }
    }); 


private void MakeMeRequest(){
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
        @Override
        public void onCompleted(JSONObject json, GraphResponse response) {
            if (response.getError() != null) {
                Log.d("XXX", "Facebook graph request error");
            } else {
                Log.d("XXX", "Facebook graph request success");

                ...
            }
        }
    });

    Bundle parameters = new Bundle();
    parameters.putString("fields", "id,name,picture.type(large),email,birthday,first_name,last_name,gender,locale");
    request.setParameters(parameters);
    request.executeAsync();
}

我很确定我忘记了什么,但我无法弄清楚是什么......

谢谢,

0 个答案:

没有答案