Hello @Joseph Burruss,
The error "401 Unauthorized" usually occurs if the Microsoft Entra ID application does not required permissions to perform the action.
- I can see you have granted
Calendars.Read
application type API permission to the Microsoft Entra ID application and still facing the error.
To resolve the error, remove and revoke all permissions of the specific Microsoft Entra ID app and grant the required permission Calendars.Read
application type API permission again:
Used the below python code to fetch the user's calendar event:
tenant_id = "TenantID"
client_id = "ClientID"
client_secret = "Secret"
user_id = "******@xxx.onmicrosoft.com"
scope = "https://graph.microsoft.com/.default"
token_url = f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token"
token_data = {
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
"scope": scope
}
token_headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
token_response = requests.post(token_url, data=token_data, headers=token_headers)
if token_response.status_code != 200:
print("Failed to get token:", token_response.status_code, token_response.text)
exit()
access_token = token_response.json()["access_token"]
graph_url = f"https://graph.microsoft.com/v1.0/users/{user_id}/calendar/events"
graph_headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.get(graph_url, headers=graph_headers)
if response.status_code == 200:
events = response.json().get('value', [])
print(f"Found {len(events)} events:")
for event in events:
print(f"- {event['subject']} ({event['start']['dateTime']} to {event['end']['dateTime']})")
else:
print("Error fetching events:", response.status_code, response.text)
If still the issue persists, try to create a new Microsoft Entra ID application from scratch and try granting only Calendars.Read
application type API permission.
- Decode the access token in jwt.ms and check if the role Calendars.Read is present.
Reference:
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.