How to get the profile picture for a Microsoft account

Viewed 3895

I'm using the MS Graph API in a web page to get the information of a user logged in to an application using a Microsoft account linked to an Azure AD directory user.

I can get their name and basic profile info using 'https://graph.microsoft.com/v1.0/me' but attempting to get their profile picture using 'https://graph.microsoft.com/v1.0/me/photo' or 'https://graph.microsoft.com/beta/me/photo' returns 404

However the Microsoft login page that appears during the login redirect does show the user's account profile picture, which also appears if that account is used to log into the Graph explorer.

The account is a Hotmail account, I don't know whether this makes it a 365 account or not. The User account in the Azure portal shows no profile picture in the User's details in the Directory - Users view, although when logged into Azure using that account the profile picture does appear in the Azure portal nav bar.

So how do I get the profile picture of a basic Microsoft account?

UPDATE

So after wading through more documentation I learned that only the beta graph api works to return the photo of a personal acount, and it only works on the data endpoint ($value) not the metadata endpoint.

So with that in mind I tested it using the Graph Explorer.

In the Graph Explorer the endpoint https://graph.microsoft.com/beta/me/photo/$value correctly returns my account's profile image, but the same request from Javascript returns 404.

Looking at the request in the Graph Explorer and browser devtools there's nothing obviously different from the request being made by the Graph Explorer and the one I'm making from Javascript, but the call from Javascript just isn't working.

Also a call to https://graph.microsoft.com/beta/me from my Javascript does work, which suggests that I'm doing nothing wrong my end.

UPDATE 2

The Graph beta API appears to work differently depending on whether the access token is retrieved for a single-tenant or multi-tenant application as described here https://github.com/microsoftgraph/microsoft-graph-explorer-v4/issues/1077

The effect of this is that you can get the profile picture for a private MS account via a multi-tenant application but not via a single tenant application. Which makes no sense at all, is not documentated, and seems like a bug.

The Graph API has had weakenesses in this area that have not been addressed for years, despite assurances.

https://github.com/microsoftgraph/microsoft-graph-docs/issues/3990

For this reason I suspect MS Identity Platform is going the same way as Azure Devops, years of development and promise, but ultimately never delivering a complete useful product.

2 Answers

You can also use powershell as follow:

Install-Module -Name AzureAD
Connect-AzureAD -AccountId "youremail@domain"
$user_id = Get-AzureADUser -ObjectId "someonemail@domain" | Select-Object -Property Objectid
Get-AzureADUserThumbnailPhoto -ObjectId $user_id -FilePath "\some\local\path\"

Microsoft Graph v1.0 retrieves the user's profilePhoto from Exchange Online, not Azure Active Directory. As such, users that don't have a mailbox provisioned in Exchange Online will not have a profilePhoto available.

From the documentation:

A profile photo of a user, group or an Outlook contact accessed from Exchange Online. It's binary data not encoded in base-64

In the /beta endpoint, the profilePhoto resource was expanded to include both Exchange and AAD. From the Beta documentation:

A profile photo of a user, group or an Outlook contact accessed from Exchange Online or Azure Active Directory (AAD). It's binary data not encoded in base-64.

And Reading and updating a user's profile photo is only possible if the user has a mailbox. Additionally, any photos that may have been previously stored using the thumbnailPhoto property (using the Office 365 unified API preview, or the Azure AD Graph, or through AD Connect synchronization) are no longer accessible through the Microsoft Graph photo property of the user resource. For more details You could check official docs here

You could try with Microsoft Graph REST API Beta version request URL in flowing format:

https://graph.microsoft.com/beta/users/UserIdOrPrincipalName/photo/$value

enter image description here

Post Man Request Example:

Request URL:

https://graph.microsoft.com/beta/users/UserIdOrPrincipalName/photo/$value

enter image description here

For more details refer this document

Related