【问题标题】:Azure Graph API invitation returning 400 Bad RequestAzure Graph API 邀请返回 400 错误请求
【发布时间】:2021-09-08 16:23:04
【问题描述】:

我有一个 Azure PowerShell 函数应用程序,它为他们创建用户、他们的资源和 RBAC,并向该用户发送邀请电子邮件。它已经工作了2年多没有改变。 3-4 周前它停止发送邀请电子邮件。我查明了罪魁祸首并在 Postman 中对其进行了模拟。看起来我这边没有明显变化 Invitation Graph API 开始发送 400 Bad Request 作为响应。 这是我的 API 调用的 sn-ps。 请求不记名令牌:

curl --location --request POST 'https://login.microsoftonline.com/{{myTenantId}}/oauth2/token' \
--header 'Content-Type: application/x-www-form-urlencoded' ' \
--header 'Cookie: brcap=0; fpc=AtYZMK7Wg2BCiTPI8GyfoR-8f7k3AQAAAJ-AsNgOAAAA; stsservicecookie=estsfd; x-ms-gateway-slice=estsfd' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id={{myClientId}}' \
--data-urlencode 'client_secret={{clientSecret}}' \
--data-urlencode 'resource=https://graph.microsoft.com' \
--data-urlencode 'username={{username}}' \
--data-urlencode 'password={{userSecret}}'

此调用返回 200 OK 和以下响应:

{
    "token_type": "Bearer",
    "scope": "Directory.AccessAsUser.All User.Invite.All User.Read",
    "expires_in": "3599",
    "ext_expires_in": "3599",
    "expires_on": "1630701785",
    "not_before": "1630697885",
    "resource": "https://graph.microsoft.com",
    "access_token": "eyJ0eXAiOiJKV1QiLCJu....

然后我使用此令牌发送带有此请求的新用户邀请:

curl --location --request POST 'https://graph.microsoft.com/v1.0/invitations' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {{token}}' \
--data-urlencode 'invitedUserEmailAddress={{userEmailAddress}}' \
--data-urlencode 'invitedUserDisplayName={{userName}}' \
--data-urlencode 'inviteRedirectUrl=https://portal.azure.com' \
--data-urlencode 'sendInvitationMessage=true'

这将返回 400 Bad Request 和以下错误详细信息:

{"error":{"code":"UnknownError","message":"Bad Request","innerError":{"date":"2021-09-03T14:33:51","request-id":"f5cffbdd-af17-445b-bb3f-85a59bac6f31","client-request-id":"f5cffbdd-af17-445b-bb3f-85a59bac6f31"}}}

用于获取 Bearer 令牌的应用注册具有 User.Invite.All API 权限,并且用户已分配全局管理员角色。

我通过 Azure 支持调查了这个问题,但到目前为止我们还没有解决方案。任何如何解决它的建议将不胜感激。

【问题讨论】:

    标签: azure azure-active-directory microsoft-graph-api invitation


    【解决方案1】:

    您正在传递 url 编码变量,the documentation 指出您需要发送 JSON 正文。我猜想支持 url 编码变量首先是一个错误,现在已经修复。

    您的第二个脚本应如下所示:

    curl --location --request POST 'https://graph.microsoft.com/v1.0/invitations' \
    --header 'Content-Type: application/json' \
    --header 'Authorization: Bearer {{token}}' \
    --data-raw '{"invitedUserEmailAddress": "{{userEmailAddress}}", "inviteRedirectUrl": "https://portal.azure.com"}'
    

    注意:在 Windows 上,您可能需要在使用 cURL 时使用双引号并转义 JSON 正文。

    【讨论】:

    • 感谢@tiamo 的回答。我在 Postman 中尝试了你的建议,这确实是罪魁祸首。用 --data-raw 替换 --data-urlencode 有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多