Microsoft Graph访问OneNote作为守护程序服务返回无效范围

时间:2020-08-18 06:34:46

标签: python microsoft-graph-api

我正试图通过守护程序服务从组织中的用户访问onenote页面,因此用户将无法登录,它将运行的后台服务。

我或多或少地遵循了本教程:https://docs.microsoft.com/en-us/graph/auth-v2-service

我已经设置了必要的API权限,并且已由管理员授予。 enter image description here

然后我请求一个运行正常的OAuth令牌。然后我发送对Onenote页面的请求

import requests

payload = {
    'client_id': '<my_client_id_is_here>',
    'scope': 'https://graph.microsoft.com/.default',
    'client_secret': '<my_client_secret_is_here>',
    'grant_type': 'client_credentials'
},

 tenant_id = "<my_tenant_id_is_here>"

response = requests.post(
    url='https://login.microsoftonline.com/{0}/oauth2/v2.0/token'.format(
        tenant_id
    ),
    data=payload 
)
token = json.loads(response.content)

test_url = 'https://graph.microsoft.com/v1.0/sites/<my-sharepoint-site-id>/onenote/sections/<my-onenote-section-id>/pages?top=100'


response = requests.get(
    url=test_url,
    headers={
        'Authorization': 'Bearer ' + token['access_token']
    }
)
print(response.content)
   

当我打印响应时,我得到:

{
    "error": {
        "code": "40004",
        "message": "The OAuth token provided does not have the necessary scopes to complete the request. Please make sure you are including one or more of the following scopes: Notes.ReadWrite.All,Notes.Read.All",
            "innerError": {
            "date": "2020-08-18T06:17:51",
            "request-id": "1042af2a-ddc8-4c72-84b2-56f0079d6174"
        }
    }
}

我尝试将scope更改为'scope': 'https://graph.microsoft.com/notes.readwrite.all',,但是令牌请求不起作用。出现错误消息“范围参数无效”

1 个答案:

答案 0 :(得分:0)

解决了,我不小心选择了旧版OneNote API端点作为我的权限,而不是我尝试使用的Microsoft Graph OneNote端点。