How to access OneNote Notebook data with MS Graph SDK python

Yon Gold 0 Reputation points
2024-05-02T11:54:36.8833333+00:00

Im trying to build a python app/script that goes through a onenote notebook and create folders for each section and creates html files for each of the pages in the section that will be saved in the respective folder.

I tried to play around with the python msgraph-sdk but i cant figure out how to get actual notebook data like the one in the json responses in the graph explorer website

im copying some code snippets directly from graph explorer as well and all im getting is 400 bad request errors

credentials = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=secret_id)

scopes = ["https://graph.microsoft.com/.default"]
client = GraphServiceClient(credentials=credentials, scopes=scopes)

async def main():
	
	apps = await client.applications.get()
	    if apps and apps.value:
	        for app in apps.value:
	            print(app.display_name)
	    # Gets registered app name as expected
	
	    await client.me.onenote.pages.by_onenote_page_id(
	        "0-e4480296df44483797a6e9498a27ee08!240-2D4AD3C4F6B959E4!363379",
	    ).content.get()
	    # 400 Bad request
	    # msgraph.generated.models.o_data_errors.o_data_error.ODataError:
	    # APIError
	    # Code: 400
	    # message: None

asyncio.run(main())

Ive registered the following permissions in my azure app registration :

Notes.Read.All

Application.Read.All

User.Read

Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. CarlZhao-MSFT 46,376 Reputation points
    2024-05-03T03:11:19.7966667+00:00

    Hi @Yon Gold

    The /me endpoint is only available in a delegated context to refer to the logged in user. If you are using an application-only context, then you should call the /users/{user_id} endpoint.

    from msgraph import GraphServiceClient
    
    graph_client = GraphServiceClient(credentials, scopes)
    
    
    result = await graph_client.users.by_user_id('user-id').onenote.pages.get()
    

    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.


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.