上传用户照片Microsoft图形API时出错

时间:2019-04-03 18:22:43

标签: .net vb.net office365 microsoft-graph

我使用应用程序许可权流程进行认证,并具有API权限User.ReadWrite.All

我想用存储在单独数据库中的图像更新/同步用户的个人资料图像。

出于调试目的,我从“旧式” Windows窗体图片框中的日期数据库中检索了图像。然后,将使用以下代码将图像传输到Office 365:

 Dim pic = New MemoryStream
 PictureBox1.Image.Save(pic, Imaging.ImageFormat.Jpeg)

 Dim Photo = Await graphServiceClient.Users(TxtEmail.Text).Photo.Content.Request().PutAsync(pic)

这引起了人们的理解:

Microsoft.Graph.ServiceException
  HResult=0x80131500
  Message=Code: ErrorItemNotFound
Message: The specified object was not found in the store., No photo with class 'IPM.UserPhoto.Preview' exists.

Inner error

  Source=Microsoft.Graph.Core
  StackTrace:
   at Microsoft.Graph.HttpProvider.<SendAsync>d__18.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.BaseRequest.<SendRequestAsync>d__34.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Graph.BaseRequest.<SendStreamRequestAsync>d__32.MoveNext()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Firebird_Test_Photo.Form1.VB$StateMachine_5_BtnUpdateExchanhe_Click.MoveNext() in C:\Users\jodur.KREMER\source\repos\Firebird Test Photo\Firebird Test Photo\Form1.vb:line 125

下面的用于将图像检索到图片框的模拟代码很好用

Dim Photo = Await graphServiceClient.Users(TxtEmail.Text).Photo.Content.Request().GetAsync()
PictureBox2.Image = System.Drawing.Image.FromStream(Photo)

使用此功能检索的流更新照片也会导致相同的异常,因此我不怀疑流无效。

对此问题有何建议??也欢迎使用C#提出建议!

1 个答案:

答案 0 :(得分:0)

$httpClient = [System.Net.Http.HttpClient]::new()
$httpClient.DefaultRequestHeaders.Authorization = [String]::Format('Bearer {0}', $accessToken)
$uri = [System.Uri]::new("https://graph.microsoft.com/v1.0/users/$($AzUserUPN)/photo/$value")

# Prepare Content
$content = [System.Net.Http.ByteArrayContent]::new([System.IO.File]::ReadAllBytes($AzUserImage));
$content.Headers.ContentType = "image/jpeg";

$task = $httpClient.PutAsync($uri,$content)
$task.Wait()