首页 > 解决方案 > 在 android 中使用 facebook 登录后无法获取 facebook 用户个人资料

问题描述

我正在集成 Facebook 登录集成。登录后,我需要获取用户的姓名、电子邮件和个人资料图片。我正在获取详细信息,但个人资料图片未显示在我的图像视图 android 中。

 GraphRequest request = GraphRequest.newMeRequest(
                    currentAccessToken, new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.d("TAG", object.toString());
                            Log.d("res", object.toString());
                            Log.d("res_obj", response.toString());
                            try {
                                String id = object.getString("id");
                                try {
                                    URL profile_pic = new URL("https://graph.facebook.com/" + id + "/picture?width=50&height=50");
                                    Log.i("profile_pic", profile_pic + "");
                                    String f_name = object.getString("first_name");
                                    String l_name = object.getString("last_name");
                                    String name = f_name + " " + l_name;
                                    String email = object.getString("email");
                                    String image = profile_pic.toString();
                                    Log.d("data", email + name + image);
                                    updateUI(name,email,image);
    
                                } catch (MalformedURLException e) {
                                   Log.e(TAG,"pic res"+e.getMessage()) ;
                                }
    
    
                            } catch (JSONException e) {
    
                                e.printStackTrace();
    
                            }
                        }
                    });
    
            Bundle parameters = new Bundle();
            parameters.putString("fields", "first_name,last_name,email,id,picture");
            request.setParameters(parameters);
            request.executeAsync();

// 在这里我得到了所有的细节,但图像没有上传,而且我也遇到了这样的异常。

{HttpStatus:400,errorCode:100,subErrorCode:33,errorType:GraphMethodException,errorMessage:不支持的获取请求。ID 为“502053987890832”的对象不存在,由于缺少权限而无法加载,或者不支持此操作。请阅读 https://developers.facebook.com/docs/graph-api}上的 Graph API 文档

标签: javaandroidfacebook

解决方案


我认为文档非常明确是什么问题:

Querying a User ID (UID) now requires an access token. Refer to the requirements table to determine which token type to include in UID-based requests.

这将带您进行解释。阅读本文,您似乎需要使用用户访问令牌。文档告诉您如何在此处获取访问令牌。

遗憾的是 Facebook API 文档并没有明确说明如何添加访问令牌,假设所有用户都知道 OAuth 2.0 的细节。不过,您可以简单地向您的请求添加Authorization带有值的标头,这将授权您的访问。Bearer <you-access-token>


推荐阅读