读取 Outlook 日历 [该请求对应用程序的 'userAudience' 配置无效。]

Reading Outlook Calendars [The request is not valid for the application's 'userAudience' configuration.]

今天我正在尝试从 outlook 中读取日历。我通过 Microsoft Azure 创建了一个新应用程序,然后设置了一个密钥并添加了 api 权限。当我尝试通过简单的脚本进行身份验证时,我发现了一个错误

The request is not valid for the application's 'userAudience' configuration. 
In order to use /common/ endpoint%2c the application must not be configured with 'Consumer' as the user audience. 
The userAudience should be configured with 'All' to use /common/ endpoint

这是我的脚本

from O365 import Account, MSGraphProtocol

CLIENT_ID = 'MY CLIENT ID'
SECRET_ID = 'MY SECRET ID'

credentials = (CLIENT_ID, SECRET_ID)
protocol = MSGraphProtocol()
scopes = ['Calendars.Read.Shared']
account = Account(credentials, protocol=protocol)

if account.authenticate(scopes=scopes):
    print('Authenticated!')

你能告诉我这个错误的原因吗?我应该如何解决它?

您似乎在尝试使用 client_credentials 流程,但您首先使用的是不正确的委托权限。因此,在您的应用程序注册中,您需要确保已为日历分配应用程序权限,例如

要使用 Client_credentials 流程,您需要首先找到您的租户 ID(如果您还不知道),您可以在 python 中执行此操作,例如使用请求(您需要替换 yourdomain.com 使用您使用的域名

requests.get('https://login.windows.net/yourdomain.com/v2.0/.well-known/openid-configuration').json()["token_endpoint"]

然后取响应的guid部分eg

'https://login.windows.net/1c3a18bf-da31-4f6c-xxxx-2c06c9cf5ae4/oauth2/v2.0/token'

那么你的代码应该看起来像 where

from O365 import Account

credentials = ('my_client_id', 'my_client_secret')

# the default protocol will be Microsoft Graph

account = Account(credentials, auth_flow_type='credentials', tenant_id='1c3a18bf-da31-4f6c-xxxx-2c06c9cf5ae4')
if account.authenticate():
   print('Authenticated!')