Use Facebook App-scoped Id for ProfilePictureView

為{幸葍}努か 提交于 2019-12-17 20:33:16

问题


I am implementing facebook's SDK for an android app. I have a ProfilePictureView, that requires, to be filled:

profilePictureView.setProfileId(id)

Yesterday, if I used the App-scoped Id of the profile (the one returned by Graph API) it worked: that's the only id I have, because apparently is against Facebook policy to get the "real" id.

But today, without any modification, it returns a null image. I am guessing they changed the backend, but if so, what is the purpose of ProfilePictureView if it requires an id that is against Facebook's policy to get?


回答1:


From 26 Mar 2018, all solutions related to manual link don't work anymore

Use the code below

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



回答2:


I will answer myself, since this question specifically asks for "getting a profile picture given the id", and the enlightening answer of Levon works only for the current user.

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


来源:https://stackoverflow.com/questions/49516674/use-facebook-app-scoped-id-for-profilepictureview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!