Skip to content

Instantly share code, notes, and snippets.

@shemogumbe
Created April 19, 2024 12:57
Show Gist options
  • Save shemogumbe/4b73bed1ca76c88b82b87e46f4998dbb to your computer and use it in GitHub Desktop.
Save shemogumbe/4b73bed1ca76c88b82b87e46f4998dbb to your computer and use it in GitHub Desktop.
Upload a profile phto for a user
import requests
# Read binary data from file
with open("binary_img.txt", "rb") as f:
binary_data = f.read()
# Endpoint URL
url = "https://graph.microsoft.com/v1.0/me/photo/$value"
# Token (replace with your actual token)
token = "<your-token-here>"
# Headers
headers = {
"Authorization": f"Bearer {token}",
"Content-Type":
"image/jpeg" # Adjust content type based on your image format
}
# Send PATCH request with binary data
response = requests.patch(url, headers=headers, data=binary_data)
# Check response status
if response.status_code == 204:
print("Profile photo updated successfully!")
else:
print("Failed to update profile photo. Status code:", response.status_code)
print("Response:", response.text)
@shemogumbe
Copy link
Author

Gives a success message:
"Profile photo updated successfully!
and the profile photo is visible via:
https://graph.microsoft.com/v1.0/me/photo/$value is Graph explorer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment