microsoft graph 上的发送邮件 API 上的令牌错误

问题描述 投票:0回答:1

我正在尝试使用 Microsoft Graph API 来使用发送邮件 API,并且我已经使用 Graph API 资源管理器进行了尝试,并且工作正常,但是当我尝试在 Postman 上获取令牌并获取令牌时,出现错误。 当我在代码中使用资源管理器中的令牌时,它工作正常,但是当我使用 Postman 中的令牌时,我收到此错误:

响应内容:b'{"error":{"code":"BadRequest","message":"/me 请求仅在委托身份验证流程下有效。","innerError":{"date":"2024- 05-28T11:34:23","请求 ID":"","客户端请求 ID":""}}}'

import requests

url = 'https://graph.microsoft.com/v1.0/me/sendMail'

access_token = ''

headers = {
  'Authorization': 'Bearer {}'.format(access_token),
  'Content-Type': 'application/json'
}

email = {
   "message": {
       "subject": "PYTHON",
       "body": {
          "contentType": "HTML",
          "content": ""
    },
    "toRecipients": [
        {
            "emailAddress": {
                "address": "[email protected]"
            }
        }
    ]
},
"saveToSentItems": "true"
}
response = requests.post(url, headers=headers, json=email)

if response.status_code == 202:
   print("Email sent successfully.")
else:
  print("Failed to send email. Status code:", response.status_code)
  print("Response content:", response.content)

enter image description here

enter image description here/OhHtbv18.png

python azure microsoft-graph-api access-token
1个回答
0
投票

发生错误的原因是

/me
端点仅支持委托流程,例如涉及用户交互的授权代码流程,但客户端凭据是仅应用程序流程。

最初,当我运行传递使用客户端凭据流生成的令牌的代码时,我也遇到了相同的错误,如下所示:

import requests

url = 'https://graph.microsoft.com/v1.0/me/sendMail'
access_token = 'token'

headers = {
  'Authorization': 'Bearer {}'.format(access_token),
  'Content-Type': 'application/json'
}

email = {
   "message": {
       "subject": "PYTHON",
       "body": {
          "contentType": "HTML",
          "content": ""
    },
    "toRecipients": [
        {
            "emailAddress": {
                "address": "[email protected]"
            }
        }
    ]
},
"saveToSentItems": "true"
}
response = requests.post(url, headers=headers, json=email)

if response.status_code == 202:
   print("Email sent successfully.")
   
else:
  print("Failed to send email. Status code:", response.status_code)
  print("Response content:", response.content)

回复:

enter image description here

要解决该错误,您需要切换到 dele脱 流(例如用于生成令牌的授权代码流),或使用

/users/userID
端点和客户端凭据令牌。

就我而言,我在应用程序注册中授予了

Mail.Send
应用程序类型权限,如下所示:

enter image description here

现在,我通过 Postman 使用客户端凭据流生成令牌,如下所示:

POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token

grant_type:client_credentials
client_id: appId
client_secret: secret
scope: https://graph.microsoft.com/.default

回复:

enter image description here

当我通过将端点更改为

/users/userID
在下面的 Python 代码中使用此令牌时,我得到了如下响应:

import requests

url = 'https://graph.microsoft.com/v1.0/users/userId/sendMail'
access_token = 'token'

headers = {
  'Authorization': 'Bearer {}'.format(access_token),
  'Content-Type': 'application/json'
}

email = {
   "message": {
       "subject": "PYTHON",
       "body": {
          "contentType": "HTML",
          "content": ""
    },
    "toRecipients": [
        {
            "emailAddress": {
                "address": "[email protected]"
            }
        }
    ]
},
"saveToSentItems": "true"
}
response = requests.post(url, headers=headers, json=email)

if response.status_code == 202:
   print("Email sent successfully.")
   
else:
  print("Failed to send email. Status code:", response.status_code)
  print("Response content:", response.content)

回复:

enter image description here

您可以在 Azure 门户中找到发件人的用户 ID,如下所示:

enter image description here

/me
端点在 Graph Explorer 中工作,因为它使用登录用户的委派权限。如果您更喜欢切换到委托流来生成令牌,请参阅我之前回答过的类似SO线程,其中包括带有授权代码流的令牌生成:

azure Active Directory - 调用microsoft graph - Thinbug

© www.soinside.com 2019 - 2024. All rights reserved.