0

我正在尝试使用MS Graph API v1.0发送电子邮件:

Invoke-RestMethod
    -Uri "https://graph.microsoft.com/v1.0/users/XXX/sendMail" 
    -Method Post
    -ContentType 'application/json'
    -Body $body
    -Headers @{ Authorization = "Bearer $token" }

在哪里:

  • XXX是我的用户的主体名称(在我的租户中),格式为:my.user_myorg.com#EXT#@myorg.onmicrosoft.com.
  • $token是我得到的访问令牌。它有Mail.Send我检查过的声明。
  • $body是下面的 JSON。
{
  "saveToSentItems": false,
  "message": {
    "toRecipients": [
      {
        "emailAddress": {
          "address": "YYY"
        }
      }
    ],
    "body": {
      "content": "Hello there",
      "contentType": "Text"
    },
    "subject": "Hello test"
  }
}

运行命令时,我得到 405:

{
    "error": {
        "code": "Request_BadRequest",
        "message": "Specified HTTP method is not allowed for the request target.",
        "innerError": {
            "date": "...",
            "request-id": "...",
            "client-request-id": "..."
        }
    }
}

尝试

如果我在 中使用我的用户 ID 而不是主体名称XXX,我会得到 404:

{
    "error": {
        "code": "ResourceNotFound",
        "message": "Resource could not be discovered.",
        "innerError": {
            "date": "...",
            "request-id": "...",
            "client-request-id": "..."
        }
    }
}

请注意,如果我尝试通过 API 使用 ID 或主体名称让我的用户https://graph.microsoft.com/v1.0/users/{id|principalName},我可以成功检索它。

4

0 回答 0