简体   繁体   中英

Update user profile photo with Microsoft Graph .NET SDK

I have a problem with updating or setting a user profile picture in Microsoft graph using the Microsoft.Graph .net SDK.

Scenario:

Native app with user (user role) present including a user profile page allowing the user to update his profile.

Azure AD v2.0 endpoint used & app registration done accordingly. App is used by many tenants, therefore we have Admin Consent flow included in the apps sign-up flow.

The scopes consented are User.ReadWrite , Directory.AccessAsUser.All , Directory.ReadWrite.All , and User.ReadWrite.All .


When uploading a User profile picture using the library, we would expect the Content-Type to be image/jpeg per the documentation . Fiddler however shows content-type as application/octet-stream

PUT /v1.0/me/photo/$value HTTP/1.1
SdkVersion: Graph-dotnet-1.7.0
Cache-Control: no-store, no-cache
Authorization: bearer //removed//
Content-Length: 1051534
Content-Type: application/octet-stream
Host: graph.microsoft.com
Connection: Keep-Alive

Result is strange as I think its miss interpreted by graph

{
  "error": {
    "code": "ErrorInternalServerError",
    "message":
      "An internal server error occurred. The operation failed., The value is set to empty\r\nParameter name: smtpAddress",
    "innerError": {
      "request-id": "5b549f03-1ce5-4c8c-a393-27aef3ed1a75",
      "date": "2018-02-07T12:32:26"
    }
  }
}

The code I'm using looks like this:

if (_selectedPhoto != null)
{
    using(IRandomAccessStream raStream = await _selectedPhoto.OpenReadAsync())
    {
        await graphClient
            .Me
            .Photo
            .Content
            .Request()
            .PutAsync(raStream.AsStream());
    }
}

I addition I cloned the SDK repository and ran the Unit Test UserUpdatePhoto with the same negative result.

The corresponding method is below. Changing content type to image/jpeg does result in a 503 unknown error . Not sure what the root cause is here.

public System.Threading.Tasks.Task<Stream> PutAsync(Stream content, CancellationToken cancellationToken, HttpCompletionOption completionOption = HttpCompletionOption.ResponseContentRead)
{
    this.ContentType = "application/octet-stream";
    this.Method = "PUT";
    return this.SendStreamRequestAsync(content, cancellationToken, completionOption);
}

As per comments, I checked the Mailbox of an effected user and there was an issue with the mailbox provisioning. So the indication of a missing SMTP address was right and the issue is only due to a missing mailbox.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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