使用Facebook App-scoped Id for ProfilePictureView

时间:2018-03-27 15:17:30

标签: android facebook facebook-sdk-4.0

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

profilePictureView.setProfileId(id)

昨天,如果我使用了配置文件的App-scoped Id(由Graph API返回的那个),它就起作用了:这是我唯一拥有的ID,因为显然是违反了Facebook的政策来获得&#34 ;实"标识。

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

2 个答案:

答案 0 :(得分:2)

从2018年3月26日起,所有与手动链接相关的解决方案都不再适用

使用以下代码

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;
    }

答案 1 :(得分:0)

我会自己回答,因为这个问题特别要求"获得一张带有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;
}