0
votes

I am looking for a sample REST Client that can update user thumbnailphoto using Azure AD graph API? REST Client to Get is there and it works https://msdn.microsoft.com/en-us/library/azure/ad/graph/api/users-operations#GetUserThumbnailPhoto

I tried this sample Java Rest Client but Received 405 - Method Not Allowed:

 public void updateUserPhotoGraph(ModelMap model) throws IOException {

        //https://graph.windows.net/{tenant}/users/{user}/thumbnailPhoto?api-version=1.6
        UriComponents uriComponents = getPhotoUri();
        String bearerToken = getBearerToken();

        try {

            HttpClient httpclient = HttpClients.createDefault();
            byte[] bytesEncoded = Base64.encode(extractBytes());

            URIBuilder builder = new URIBuilder(uriComponents.toString());
            URI uri = builder.build();
            HttpPost request = new HttpPost(uri);
            request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + bearerToken);
            request.setHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM);
            request.setEntity(new ByteArrayEntity(bytesEncoded));

            HttpResponse response = httpclient.execute(request);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                System.out.println(EntityUtils.toString(entity));
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

** Changed the above to sent PATCH request as well, but got the same error.

Anyone used this API to update thumnailphoto?
Can we use [https://graph.windows.net/{tenant}/users/{user}/thumbnailPhoto?api-version=1.6] to Update/Set Thumbnail photo?

What would be the the right API for that?

3

3 Answers

1
votes

With help of the above answers I got a working solution in c#. This methode works when called with the following variables in place:

nuget: ADAL v2.19.

authContext = new AuthenticationContext("https://login.microsoftonline.com/" + tenant)

Globals.aadGraphResourceId = "https://graph.windows.net/"

credential = new ClientCredential(clientId, clientSecret)

api = "/users/" + objectId + "/thumbnailPhoto"

Hope it helps!

private async Task<string> UploadByteArray(string api, byte[] byteArray)
{
        // NOTE: This client uses ADAL v2, not ADAL v4
        AuthenticationResult result = authContext.AcquireToken(Globals.aadGraphResourceId, credential);
        HttpClient http = new HttpClient();
        string url = Globals.aadGraphEndpoint + tenant + api + "?" + Globals.aadGraphVersion;

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Put, url);
        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);            
        request.Content = new System.Net.Http.ByteArrayContent(byteArray);
        request.Content.Headers.Add("Content-Type", "application/octet-stream");
        HttpResponseMessage response = await http.SendAsync(request);

        if (!response.IsSuccessStatusCode)
        {
            string error = await response.Content.ReadAsStringAsync();
            object formatted = JsonConvert.DeserializeObject(error);
            throw new WebException("Error Calling the Graph API: \n" + JsonConvert.SerializeObject(formatted, Formatting.Indented));
        }

        return await response.Content.ReadAsStringAsync();
    }
0
votes

When looking into this document, you could find "thumbnailPhoto" property is stream type and could use PATCH , so you may try to use below api to update that property :

 https://graph.windows.net/{tenant}/directoryObjects/{user}/Microsoft.DirectoryServices.User/thumbnailPhoto?api-version=1.5
0
votes

I was able to upload a Jpeg thumbnail using a PUT operation to the url: https://graph.windows.net/{tenant}/users/{user object id}/thumbnailPhoto?api-version=1.6

The content type header needs to be set to application/octet-stream and the content is just the binary Jpeg data.

My code is in Swift, so I won't show it here, but I presume given this information it shouldn't be too hard to create the appropriate Java code.