accessing onenote via API

Daniele 0 Reputation points
2024-10-27T11:22:09.7333333+00:00

Is it possible to access my onenote through python API for free or I have to subscribe to Azure in a paied plan?

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Vinodh247 34,661 Reputation points MVP Volunteer Moderator
    2024-10-27T13:23:15.7+00:00

    Hi Daniele,

    Thanks for reaching out to Microsoft Q&A.

    To access OneNote through an API, you don’t need an Azure subscription, as OneNote is part of the Microsoft Graph API suite, which allows free access to certain endpoints, including OneNote resources. You can connect to OneNote through Microsoft Graph API using Python without an Azure paid plan. However, you will need:

    1. Microsoft 365 Account: Access to OneNote APIs requires authentication via a Microsoft 365 account.
    2. Registered App in Azure AD: To enable API access, you’ll need to register an application in Azure Active Directory (AAD), which is free and does not require a paid Azure subscription. This allows you to obtain client credentials for authentication.

    Here’s a quick outline of how you can set this up:

    Register an App in Azure AD:

    • Go to the Azure portal, navigate to Azure Active Directory > App registrations, and create a new registration.
      • Configure permissions for the Microsoft Graph API to access OneNote notebooks.
    1. Use Python to Access OneNote:
    • Use the requests library in Python or the msal library (Microsoft Authentication Library for Python) to authenticate with the Microsoft Graph API.
      • Make API requests to access OneNote resources (e.g., list notebooks, sections, pages).

    Here’s a simple Python example for authenticating and accessing OneNote with Microsoft Graph:

    import msal
    import requests
    # App details from Azure AD
    client_id = 'YOUR_CLIENT_ID'
    client_secret = 'YOUR_CLIENT_SECRET'
    authority = 'https://login.microsoftonline.com/YOUR_TENANT_ID'
    scope = ['https://graph.microsoft.com/.default']
    # Authenticate
    app = msal.ConfidentialClientApplication(client_id, authority=authority, client_credential=client_secret)
    token_response = app.acquire_token_for_client(scopes=scope)
    access_token = token_response.get('access_token')
    # Access OneNote API
    headers = {
        'Authorization': 'Bearer ' + access_token,
        'Content-Type': 'application/json'
    }
    onenote_url = 'https://graph.microsoft.com/v1.0/me/onenote/notebooks'
    response = requests.get(onenote_url, headers=headers)
    if response.status_code == 200:
        print("Notebooks:", response.json())
    else:
        print("Error:", response.status_code, response.text)
    
    

    This approach lets you access OneNote content at no additional cost, as long as you have a Microsoft 365 account.

    Please feel free to click the 'Upvote' (Thumbs-up) button and 'Accept as Answer'. This helps the community by allowing others with similar queries to easily find the solution.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.