Get calendar events with an API call from python script help

aviv eldad 0 Reputation points
2024-06-19T17:39:40.3533333+00:00

Hi,

I know that it was asked a lot, but it seems that nobody figured it out.

I'm trying to do a simple thing. Get the calendar events from a user. I followed the instructions here: https://learn.microsoft.com/en-us/graph/api/calendar-list-events?view=graph-rest-1.0&tabs=http

and It just does not work. I have all the IDs I need, I opened a personal Microsoft account (currently I'm on the trial), and the account is connect to the user in the Azure AD.

This is my code:

import msal
import r
CLIENT_SECRET = "client secret"
APP_ID = "app id"
TENANT_ID = 'tenant id'
USER_ID = 'user id'
AUTHORITY_URL = f'https://login.microsoftonline.com/{TENANT_ID}'
SCOPES = ['https://graph.microsoft.com/.default']
GRAPH_ENDPOINT = f'https://graph.microsoft.com/v1.0/users/{USER_ID}/calendar/events'
GRAPH_ENDPOINT2 = f'https://graph.microsoft.com/v1.0/users/{USER_ID}'

def get_access_token():
    app = msal.ConfidentialClientApplication(
        CLIENT_ID,
        authority=AUTHORITY_URL,
        client_credential=CLIENT_SECRET,
    )

    result = app.acquire_token_for_client(scopes=SCOPES)

    if "access_token" in result:
        print(result['access_token'])
        return result['access_token']
    else:
        print("Error obtaining access token:")
        print(result.get("error"))
        print(result.get("error_description"))
        print(result.get("correlation_id"))  # You might need this when reporting a bug
        raise Exception("Could not acquire access token")


def get_calendar_events(access_token):
    headers = {
        'Authorization': f'Bearer {access_token}'
    }
    response = requests.get(GRAPH_ENDPOINT, headers=headers)

    if response.status_code == 200:
        return response.json()
    else:
        print(f"API call failed with status code {response.status_code}")
        print(response.json())
        return None


def make_graph_api_call2(access_token):
    headers = {
        'Authorization': f'Bearer {access_token}'
    }
    response = requests.get(GRAPH_ENDPOINT2, headers=headers)

    if response.status_code == 200:
        return response.json()
    else:
        print(f"API call failed with status code {response.status_code}")
        print(response.text)
        return None

if __name__ == '__main__':
    token = get_access_token()
    users_info = make_graph_api_call2(token)
    print(users_info)
    events_info = get_calendar_events(token)
    print(events_info)

The results for make_graph_api_call2 are working fine (just getting my details), but for the make_graph_api_call (the events) I get an error:
{'error': {'code': 'OrganizationFromTenantGuidNotFound', 'message': "The tenant for tenant guid 'xxxxxxx' does not exist.", 'innerError': {'oAuthEventOperationId': 'xxxxxx', 'oAuthEventcV': 'xxxxxxx', 'errorUrl': 'https://aka.ms/autherrors#error-InvalidTenant', 'requestId': 'xxxxxx', 'date': '2024-06-19T17:33:21'}}}

I gave the permission from Azure:

User's image

Can someone please help me to understand how to configure this? Maybe give me a guide for all the steps I need to make? It needs to be simple and I stuck on it over a week now.

Thank you!

Outlook
Outlook
A family of Microsoft email and calendar products.
3,420 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,436 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yakun Huang-MSFT 2,495 Reputation points Microsoft Vendor
    2024-06-20T06:00:12.24+00:00

    Hi @aviv eldad

    This is a common problem. The tenant cannot be queried because the tenant has not assigned a license.

    Try purchasing or free trial MS 365 license for your tenant, then go to Azure AD and grant that license to the logged in user.

    User's image

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

    0 comments No comments