猿问

使用 /token 端点获取授权访问令牌

我正在创建一个将使用 Microsoft Security Graph API 的 Python 应用程序。我已经按照这里提供的示例没有问题。我现在希望能够创建一个 python 应用程序,它可以在不使用 Web 浏览器的情况下获取访问令牌(并在需要时刷新它)。


到目前为止,我已经创建了一个新的应用程序SecurityEvent.Read.All,并SecurityEvent.ReadWrite.All在这两种权限Delegated Permissions和Application Permissions。然后,我在 Web 浏览器中访问以下 url 以授予我的应用程序同意并使用我的租户管理员登录:


https://login.microsoftonline.com/common/adminconsent?

        client_id=APPLICATION_ID

        &state=12345

        &redirect_uri=REDIRECT_URL  

接下来,我假设我想按照此处的步骤进行 POST 调用以获取令牌。下面是我如何做的一个例子。


d = {

    "client_id": <client_id>,

    "scope": ["https://graph.microsoft.com/.default"],

    "client_secret": <client_secret>,

    "grant_type": "client_credentials"

}

r = requests.post("https://login.microsoftonline.com/common/oauth2/v2.0/token", data=d)

以下是我收到的回复


{

  "token_type": "Bearer",

  "expires_in": 3600,

  "ext_expires_in": 0,

}

使用现在获得的访问令牌,我正在尝试调用/alerts端点。下面是它是如何完成的。


headers = {

    "Content-type": "application/json",

    "Authorization": "Bearer " + <access_token>,

}

alerts = requests.get("https://graph.microsoft.com/v1.0/security/alerts", headers=headers)


我不知何故权限错了?


慕仙森
浏览 312回答 2
2回答

小怪兽爱吃肉

为什么这会被退回给我?我错过了一步吗?您混淆了 Graph API 和 AAD Graph API。您应该遵循以下文档来实现客户端凭据流:Register your app.Configure permissions for Microsoft Graph on your app.Get administrator consent.Get an access token.Use the access token to call Microsoft Graph.以及有关如何使用Graph Security API 的更多信息。
随时随地看视频慕课网APP

相关分类

Python
我要回答