Created
April 19, 2024 12:57
-
-
Save shemogumbe/4b73bed1ca76c88b82b87e46f4998dbb to your computer and use it in GitHub Desktop.
Upload a profile phto for a user
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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