我目前可以通过 Microsoft Graph SDK for Java 成功检索用户照片(用户/照片/内容)。如果我使用 API(用户/照片),则会抛出 Microsoft.Fast.Profile.Core.Exception.ImageNotFoundException。 不幸的是,我缺少一种检查照片是否存在的方法。用户对象上的照片属性(API:/user ->user.getPhoto())始终为 null。我觉得这非常糟糕且不合逻辑。
有人曾经成功实施过这个或有想法吗?
var user = graphClient.users().byUserId(user.getId()).get();
var photo = graphClient.users().byUserId(user.getId()).photo().get(); // photo meta data
// if(user.getPhoto() != null) // always null, even tried with expand user.photo;
if (photo != null) { // throws exception if photo is empty
var photoStream = graphClient.users().byUserId(user.getId()).photo().content().get(); // works and is filled
byte[] photoBytes = photoStream.readAllBytes();
photoStream.read(photoBytes);
var base64Photo = Base64.getEncoder().encodeToString(photoBytes);
System.out.println(base64Photo);
if (photo.getAdditionalData().containsKey(MEDIA_CONTENT_TYPE)) {
var mimeType = photo.getAdditionalData().get(MEDIA_CONTENT_TYPE);
System.out.println(mimeType);
}
}
首先,用户资源不允许扩展照片。您需要为每个用户拨打电话
var photo = graphClient.users().byUserId(user.getId()).photo().get();
端点的当前行为是,如果不存在照片,则操作将返回 404 Not Found。
您需要处理未找到的错误
try {
var photo = graphClient.users().byUserId(user.getId()).photo().get();
} catch (ODataError e) {
if (e.getResponseStatusCode() == 404 && e.getError().getCode() == "ImageNotFound" ){ // photo not exist
// add photo
}
System.out.println(e.getMessage());
}