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.

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

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()

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.

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

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.

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.