Fetching User Profile Photos

Accounts 0 Reputation points
2025-06-09T17:11:18.0133333+00:00

I'd like to use the Microsoft Graph REST API to retrieve the profile photo URL of a user.

Is there an available endpoint to do this? According to docs (https://learn.microsoft.com/en-us/graph/api/profilephoto-get?view=graph-rest-1.0&tabs=http), /photo/$value returns binary data while /photo returns metadata.

Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2025-06-09T17:26:11.2566667+00:00

    you can use the graphapi explorer to demo the api call. under get started, try the my photo:

    https://developer.microsoft.com/en-us/graph/graph-explorer

    you need a valid Entra id account to run the tool. your actual application will need a token. see:

    https://learn.microsoft.com/en-us/graph/auth/auth-concepts

    0 comments No comments

  2. SrideviM 5,630 Reputation points Microsoft External Staff Moderator
    2025-06-11T12:46:37.2366667+00:00

    Hello Accounts,

    I understand you're trying to retrieve the profile photo of users using the Microsoft Graph REST API. While the API provides the /photo/$value endpoint to get the binary data of a user's profile photo, it doesn’t directly provide a URL to access the image.

    If you're looking to display the image on a webpage or allow users to access it via a URL, you would need to store the image somewhere accessible, like Azure Blob Storage, and then provide a URL for access.

    You can retrieve the binary data, upload the photo to Azure Blob Storage, and generate a URL to access the image.

    Make sure your application has ProfilePhoto.Read.All permission in Microsoft Graph to access the profile photo of a user.

    User's image

    Create one container named profile-photos in your Azure storage account with Blob access level:

    enter image description here

    Here’s the Python script that does this:

    import requests
    from azure.identity import ClientSecretCredential
    from azure.storage.blob import BlobServiceClient, ContentSettings
    
    tenant_id = 'your-tenant-id'
    client_id = 'your-client-id'
    client_secret = 'your-client-secret'
    
    storage_account_name = 'your-storage-account'
    container_name = 'profile-photos'
    blob_name = 'user-photo.jpg'
    storage_account_key = 'your-storage-account-key'
    
    def get_access_token(tenant_id, client_id, client_secret):
        credential = ClientSecretCredential(tenant_id, client_id, client_secret)
        token = credential.get_token("https://graph.microsoft.com/.default")
        return token.token
    
    def fetch_profile_photo(access_token, user_id):
        url = f'https://graph.microsoft.com/v1.0/users/{user_id}/photo/$value'
        headers = {'Authorization': f'Bearer {access_token}'}
        response = requests.get(url, headers=headers)
        
        if response.status_code == 200:
            return response.content
        else:
            print(f"Error fetching photo: {response.status_code}")
            return None
    
    def upload_photo_to_blob(photo_data, storage_account_name, container_name, blob_name, storage_account_key):
        blob_service_client = BlobServiceClient(account_url=f"https://{storage_account_name}.blob.core.windows.net", credential=storage_account_key)
        blob_client = blob_service_client.get_blob_client(container_name, blob_name)
        
        content_settings = ContentSettings(content_type='image/jpeg')
        
        try:
            blob_client.upload_blob(photo_data, overwrite=True, content_settings=content_settings)
            print(f"Photo uploaded successfully: https://{storage_account_name}.blob.core.windows.net/{container_name}/{blob_name}")
        except Exception as e:
            print(f"Error uploading photo: {e}")
    
    def main():
        access_token = get_access_token(tenant_id, client_id, client_secret)
        
        if access_token:
            user_id = 'your-user-id'  
            photo_data = fetch_profile_photo(access_token, user_id)
            
            if photo_data:
                upload_photo_to_blob(photo_data, storage_account_name, container_name, blob_name, storage_account_key)
    
    if __name__ == "__main__":
        main()
    

    enter image description here

    Once the photo is uploaded to Azure Blob Storage, you’ll have a URL like this:

    https://{your-storage-account}.blob.core.windows.net/{container-name}/{blob-name}
    

    When I opened this URL in the browser, I successfully retrieved the user’s profile photo.

    enter image description here

    Alternatively, you can also run this API call in Graph Explorer by signing in with valid user account consenting ProfilePhoto.Read.All permission:

    GET https://graph.microsoft.com/v1.0/users/userId/photo/$value
    

    enter image description here

    Let me know if you have any further queries. Happy to assist!

    Hope this helps!


    If this answers your query, do click Accept Answer and Yes for was this answer helpful, which may help members with similar questions.

    User's image

    If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.