似乎无法得到facebook朋友的照片?

时间:2015-11-10 09:08:02

标签: android facebook facebook-graph-api

我试图通过Graph API获取我的Facebook Friend的照片。这是我的尝试:

public void getFacebookData(final AccessToken accessToken){
    //newMeRequest = My own data
    //myFriendsRequest = my mutual friends who have the app downloaded
    //Basically make 2 requests to one's Facebook info and return the names, links, id, and picture of the individual

    AccessToken accesstoken = AccessToken.getCurrentAccessToken();

    JSONArray friends = new JSONArray();
    GraphRequestBatch batch = new GraphRequestBatch(
            GraphRequest.newMeRequest(
                    AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(
                                JSONObject jsonObject,
                                GraphResponse response) {
                            // Application code for user                            
                        }
                    }),
            GraphRequest.newMyFriendsRequest(
                    AccessToken.getCurrentAccessToken(),
                    new GraphRequest.GraphJSONArrayCallback() {
                        @Override
                        public void onCompleted(JSONArray jsonArray,
                                                GraphResponse response) {
                            // Application code for users friends
                            // Insert into our local DB
                            Toast.makeText(login.this, "" + response.toString(), Toast.LENGTH_SHORT).show();
                            try {
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject row = jsonArray.getJSONObject(i);
                                    System.out.println("Got id");
                                    datasource.createFriend(row.getString("name"), row.getString("id"));
                                    new GraphRequest(
                                            AccessToken.getCurrentAccessToken(),
                                            "/" + row.getString("id") + "/picture",
                                            null,
                                            HttpMethod.GET,
                                            new GraphRequest.Callback() {
                                                public void onCompleted(GraphResponse response) {
                                                    System.out.println("Got photo");
                                                }
                                            }
                                    );
                                }
                            } catch (JSONException e) {
                                throw new RuntimeException(e);
                            }
                            System.out.println("FriendsDB: " + jsonArray);
                        }
                    }
            )
    );
    batch.addCallback(new GraphRequestBatch.Callback() {
        @Override
        public void onBatchCompleted(GraphRequestBatch graphRequests) {
            // Application code for when the batch finishes
            Log.d(TAG, graphRequests.toString());

        }
    });
    batch.executeAsync();

然而,问题是我似乎永远不会打印"Got Photo"消息。我很困惑,因为我查看了GraphRequest构造函数,发现这种方法最适合我。但是,该方法永远不会完成,我真的不确定它为什么会失败。任何帮助,将不胜感激。谢谢!

编辑:我已经确定我的应用程序已经过身份验证并具有以下权限:

loginButton.setReadPermissions(Arrays.asList("user_friends", "public_profile", "email"));

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试获取已登录用户的所有朋友,从Graph API v2开始,您无法通过致电USER_ID /朋友获取朋友列表。现在,这将返回使用该应用程序或已授权自己使用特定Facebook应用程序的用户朋友。

可以找到有关此内容的详细信息here

关于用户的照片,请阅读文档here

修改1

对于个人资料图片,从图API和

中获取用户ID

在xml文件中添加ProfilePictureView

    <com.facebook.widget.ProfilePictureView
   android:id="@+id/friendProfilePicture"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:gravity="center_horizontal"
   android:padding="5sp"
   facebook:preset_size="small" />

现在从Java添加用户的id如下: -

ProfilePictureView profilePictureView;
profilePictureView = (ProfilePictureView) findViewById(R.id.friendProfilePicture);
profilePictureView.setProfileId(userId);

使用Picasso获取图片并使用此链接将其添加到ImageView

https://graph.facebook.com/{facebookId}/picture?type=large