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:
- Microsoft 365 Account: Access to OneNote APIs requires authentication via a Microsoft 365 account.
- 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.
- Use Python to Access OneNote:
- Use the
requests
library in Python or themsal
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.