Use Facebook App-scoped Id for ProfilePictureView

前端 未结 2 1698
死守一世寂寞
死守一世寂寞 2020-12-11 13:27

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

profilePictureView.setProfileId(         


        
相关标签:
2条回答
  • 2020-12-11 13:56

    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;
        }
    
    0 讨论(0)
  • 2020-12-11 13:56

    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;
    }
    
    0 讨论(0)
提交回复
热议问题