繁体   English   中英

Facebook Graph API /我的请求

[英]Facebook Graph API /me Request

我正在尝试将Facebook登录信息合并到我的Android应用程序中。 我遇到的问题是,我似乎无法使用额外的参数成功完成/ me请求请求。

如果我尝试自行执行/ me请求,则可以获得用户ID和名称。 我想获取其他字段,例如电子邮件,名字,姓氏等。

错误

{响应:responseCode:400,graphObject:null,错误:{HttpStatus:400,errorCode:2500,errorType:OAuthException,errorMessage:必须使用活动访问令牌来查询有关当前用户的信息。}}

尝试提出要求

我是从LoginManager的onSuccess方法中执行此操作的。

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

我认为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) {

}

由于我刚刚完成了Facebook登录并返回了姓名和电子邮件,因此我可以为您提供帮助,但是我们确实需要查看您的所有代码。 我将发布所有与您使用Facebook类的方式有关的信息。 这是我使用的部分内容(如果有帮助的话):

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

我还通过其他活动访问Facebook用户朋友,因此我在以后需要时将我的访问令牌存储在首选项中。

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

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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