Question

I use spring-social-facebook to interact with Graph API (1.0.3.RELEASE if it's metter).
And I can't find any operation to retrieve profile's image url. I found only operations which return array of bytes and they are not very convenient for the implementation.
Does any kind of operation which retrieves image url exist in Spring Social?
If not, is there any 'tidy' workaround for this?
Thanks!

Was it helpful?

Solution

Finally, I didn't find any mention of profile picture url in spring social
My solution:
Initially I planned to extend UserOperations (FacebokTemplate.userOperations) class and add new method. But it's a package-level class and doens't visible outside.
So I decided to create my own template class extending FacebookTemplate and implement the method in this way:

public String fetchPictureUrl(String userId, ImageType imageType) {
    URI uri = URIBuilder.fromUri(GRAPH_API_URL + userId + "/picture" +
            "?type=" + imageType.toString().toLowerCase() + "&redirect=false").build();

    JsonNode response = getRestTemplate().getForObject(uri, JsonNode.class);
    return response.get("data").get("url").getTextValue();
}

OTHER TIPS

i just ran into same issue, hope this will help someone else

Connection<Facebook> connection = userConnectionRepository.findPrimaryConnection(Facebook.class);
connection.createData().getImageUrl()

You could take the miniature picture in this way :

"http://graph.facebook.com/" + fbUser.getId() + "/picture?type=square"

In social API you have ability just to fetch the image binary file:

byte[] profileImage = facebook.userOperations().getUserProfileImage(imageType);

To get URL you need to have something custom (as mentioned in the post above). I took the part of code from Facebook Social API (see Facebook template source code for fetchImage) and the following utility class:

public final class FacebookUtils {

    private static final String PICTURE_PATH = "picture";

    private static final String TYPE_PARAMETER = "type";

    private static final String WIDTH_PARAMETER = "width";

    private static final String HEIGHT_PARAMETER = "height";

    private FacebookUtils() {
    }

    public static String getUserProfileImageUrl(Facebook facebook, String userId, String width, String height, ImageType imageType) {
        URIBuilder uriBuilder = URIBuilder.fromUri(facebook.getBaseGraphApiUrl() + userId + StringUtils.SLASH_CHARACTER + PICTURE_PATH);
        if (imageType != null) {
            uriBuilder.queryParam(TYPE_PARAMETER, imageType.toString().toLowerCase());
        }
        if (width != null) {
            uriBuilder.queryParam(WIDTH_PARAMETER, width.toString());
        }
        if (height != null) {
            uriBuilder.queryParam(HEIGHT_PARAMETER, height.toString());
        }
        URI uri = uriBuilder.build();
        return uri.toString();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top