简体   繁体   English

无法使用 ms graph 设置个人资料照片

[英]can't set profile photo with ms graph

I tried the following:我尝试了以下方法:

filePath = sourcePath + "ash.jpg";

byte[] fileContent = FileUtils.readFileToByteArray(new File(filePath));
String encodedString = Base64.getEncoder().encodeToString(fileContent);

imagePayload = HttpRequest.BodyPublishers.ofString(encodedString)

def url='https://graph.microsoft.com/v1.0/users/<myuser>/photo/$value';

HttpClient httpClient = HttpClient.newBuilder()
                        .version(HttpClient.Version.HTTP_2)
                        .build();

HttpRequest request = HttpRequest.newBuilder()
                      .uri(URI.create(url))
                      .method("PUT",imagePayload)
                      .header("Content-Type", "image/jpeg")
                      .header("Authorization", "Bearer " + token)
                      .build();

the return keeps telling me:回报不断告诉我:

An internal server error occurred.发生内部服务器错误。 The operation failed., The file you chose isn't an image.操作失败。,您选择的文件不是图像。 Please choose a different file.请选择其他文件。

I guess that the payload is wrong, but what does he expect exactly?我猜有效载荷是错误的,但他到底期望什么? the MS Graph documentation states that it should the binary represetation of the jpeg image MS Graph 文档指出它应该对 jpeg 图像进行二进制表示

with the following:具有以下内容:

filePath = sourcePath + "ash.jpg";

file = new File(filePath);

BufferedImage image = ImageIO.read(file);

ByteArrayOutputStream b = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", b);

byte[] jpgByteArray = b.toByteArray();

StringBuilder sb = new StringBuilder();
for (byte by : jpgByteArray)
    sb.append(Integer.toBinaryString(by & 0xFF));

imagePayloadAsString = sb.toString();

imagePayload = HttpRequest.BodyPublishers.ofString(imagePayloadAsString)

I get the binary string with Os and 1s but the webservice still says that my file is not an image.我得到了带有 Os 和 1s 的二进制字符串,但网络服务仍然说我的文件不是图像。 I must confess that I'm confused我必须承认我很困惑

This endpoint takes the raw image as input, not a base64 encoded version.此端点将原始图像作为输入,而不是 base64 编码版本。 Check the documentation .检查文档 In the request body, include the binary data of the photo in the request body.在请求正文中,将照片的二进制数据包含在请求正文中。

I see that you're using HTTPClient.我看到您正在使用 HTTPClient。 I would do like the following:我想做以下事情:

Code snippet:代码片段:

 using (HttpClient client = new HttpClient()) { var authResult = await AuthenticationHelper.Current.GetAccessTokenAsync(); if (authResult.Status.= AuthenticationStatus;Success) return. client.DefaultRequestHeaders,Add("Authorization". "Bearer " + authResult;AccessToken). Uri userPhotoEndpoint = new Uri(AuthenticationHelper;GraphEndpointId + "users/" + userIdentifier + "/Photo/$value"); StreamContent content = new StreamContent(image). content.Headers,Add("Content-Type"; "application/octet-stream"). using (HttpResponseMessage response = await client,PutAsync(userPhotoEndpoint. content)) { response;EnsureSuccessStatusCode(); } }

I finally got it.我终于明白了。 what the ms graph service awaits is just the file itself (with the header image/jpeg): So this way I managed to upload the photo: ms 图形服务等待的只是文件本身(使用 header 图像/jpeg):所以我设法上传了照片:


file = new File(filePath);

imagePayload = HttpRequest.BodyPublishers.ofFile(file.toPath())

as Dev stated, no need to convert it using base64正如开发人员所说,无需使用 base64 进行转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM