【问题标题】:How to get profile image url from Facebook with spring-social?如何使用 spring-social 从 Facebook 获取个人资料图片网址?
【发布时间】:2023-03-10 05:17:01
【问题描述】:
我使用 spring-social-facebook 与 Graph API 交互(1.0.3.RELEASE,如果它是米特的话)。
我找不到任何操作来检索个人资料的图片网址。我发现只有返回字节数组的操作,它们对实现不是很方便。
Spring Social 中是否存在任何类型的检索图像 url 的操作?
如果没有,是否有任何“整洁”的解决方法?
谢谢!
【问题讨论】:
标签:
java
facebook
spring
spring-social-facebook
【解决方案1】:
最后,我在 spring social 中没有找到任何提及个人资料图片 url
我的解决方案:
最初我计划扩展UserOperations (FacebokTemplate.userOperations) 类并添加新方法。但它是一个包级别的类,在外面是不可见的。
所以我决定创建我自己的扩展FacebookTemplate的模板类并以这种方式实现该方法:
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();
}
【解决方案2】:
我遇到了同样的问题,希望这对其他人有帮助
Connection<Facebook> connection = userConnectionRepository.findPrimaryConnection(Facebook.class);
connection.createData().getImageUrl()
【解决方案3】:
你可以这样拍微缩照片:
"http://graph.facebook.com/" + fbUser.getId() + "/picture?type=square"
【解决方案4】:
在社交 API 中,您只能获取图像二进制文件:
byte[] profileImage = facebook.userOperations().getUserProfileImage(imageType);
要获取 URL,您需要自定义一些内容(如上面的帖子中所述)。
我从 Facebook 社交 API 中获取了部分代码(请参阅 fetchImage 的 Facebook 模板源代码)和以下实用程序类:
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();
}
}