为 ProfilePictureView 使用 Facebook App-scoped Id

Use Facebook App-scoped Id for ProfilePictureView

我正在为 android 应用程序实施 facebook 的 SDK。我有一个 ProfilePictureView,需要填写:

profilePictureView.setProfileId(id)

昨天,如果我使用配置文件的 App-scoped Id(由 Graph API 返回的)它起作用了:这是我唯一拥有的 id,因为显然违反 Facebook 政策来获取 "real"id.

但是今天,在没有任何修改的情况下,它 returns 是一个空图像。我猜他们改变了后端,但如果是这样,ProfilePictureView 的目的是什么,如果它需要一个违反 Facebook 政策的 ID?

自 2018 年 3 月 26 日起,所有与手册相关的解决方案 link 不再有效

使用下面的代码

private static String FACEBOOK_FIELD_PROFILE_IMAGE = "picture.type(large)";
    private static String FACEBOOK_FIELDS = "fields";

    private void getFacebookData() {
        GraphRequest request = GraphRequest.newMeRequest(
                AccessToken.getCurrentAccessToken(),
                (object, response) -> {
                    updateAvatar(getImageUrl(response));
                });
        Bundle parameters = new Bundle();
        parameters.putString(FACEBOOK_FIELDS, FACEBOOK_FIELD_PROFILE_IMAGE);
        request.setParameters(parameters);
        request.executeAsync();
    }

    private static String FACEBOOK_FIELD_PICTURE = "picture";
    private static String FACEBOOK_FIELD_DATA = "data";
    private static String FACEBOOK_FIELD_URL = "url";
    private String getImageUrl(GraphResponse response) {
        String url = null;
        try {
            url = response.getJSONObject()
                    .getJSONObject(FACEBOOK_FIELD_PICTURE)
                    .getJSONObject(FACEBOOK_FIELD_DATA)
                    .getString(FACEBOOK_FIELD_URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

我会自己回答,因为这个问题专门要求 "getting a profile picture given the id",而 Levon 的启发性答案仅适用于当前用户。

private static String FACEBOOK_FIELD_PROFILE_IMAGE = "picture.type(large)";
private static String FACEBOOK_FIELDS = "fields";
private static String FACEBOOK_FIELD_PICTURE = "picture";
private static String FACEBOOK_FIELD_DATA = "data";
private static String FACEBOOK_FIELD_URL = "url";

public static void getFacebookProfileUrl(String id, final Response<String> response) {
    GraphRequest request = GraphRequest.newGraphPathRequest(
            AccessToken.getCurrentAccessToken(),
            "/" + id,
            new GraphRequest.Callback() {
                @Override
                public void onCompleted(GraphResponse graph) {
                    String imgUrl = getImageUrl(graph);
                    // imgUrl is the url of the profilepic of the user with given id. 
                    // Works both for "real" id and "app-scoped" id
                }
            });

    Bundle parameters = new Bundle();
    parameters.putString(FACEBOOK_FIELDS, FACEBOOK_FIELD_PROFILE_IMAGE);
    request.setParameters(parameters);
    request.executeAsync();
}


private static String getImageUrl(GraphResponse response) {
    String url = null;
    try {
        url = response.getJSONObject()
                .getJSONObject(FACEBOOK_FIELD_PICTURE)
                .getJSONObject(FACEBOOK_FIELD_DATA)
                .getString(FACEBOOK_FIELD_URL);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return url;
}