简体   繁体   English

尝试使用 Graph API 发送邮件时出现 405/404 错误(OAuth2 客户端凭据流)

[英]Get 405/404 error when trying to send mails with Graph API (OAuth2 client credentials flow)

I'm making a Java application to send Mail as any user in a Microsoft tenant, with Graph APIs, so I'm using the client credentials flow (no login, automatic send).我正在制作一个 Java 应用程序,以使用图形 API 以 Microsoft 租户中的任何用户身份发送邮件,因此我使用的是客户端凭据流(无需登录,自动发送)。 I've registered an app in Azure AD giving the following application permissions (not delegated) , checking out the admin consent for every item:我在 Azure AD 中注册了一个应用程序,提供以下应用程序权限(未委托) ,检查每个项目的管理员同意:

  • Mail.Send邮件.发送
  • Mail.ReadWrite邮件读写
  • User.Read.All用户.读取.全部

For semplicity, I've tried also with calls in Postman, but I have the same issue as in the Java app.为了简单起见,我也尝试过拨打 Postman,但我遇到了与 Java 应用程序相同的问题。 I take for granted that I have got a valid access token (If I try with an invalid one, I get a 401: Unauthorized ).我理所当然地认为我有一个有效的访问令牌(如果我尝试使用无效的访问令牌,我会得到一个401: Unauthorized )。 The following is the decoded access token that I get from https://jwt.ms/ , with all the scopes included :以下是我从https://jwt.ms/获得的解码访问令牌,包括所有范围

...
  "roles": [
    "Mail.ReadWrite",
    "User.Read.All",
    "Mail.Send"
  ],
...

This is my code in Java:这是我在 Java 中的代码:

String user = "/users/<my user id or my user principal name>";
    
    UserRequestBuilder defaultUser = new UserRequestBuilder(graphClient.getServiceRoot() + user, graphClient, null);
    
    //graphClient.me()
    defaultUser
        .sendMail(UserSendMailParameterSet
            .newBuilder()
            .withMessage(message)
            .withSaveToSentItems(saveToSentItems)
            .build())
        .buildRequest()
        .post();

I can't use the "me" target ( me() method) because this is client credentials flow, so there isn't a logged user.我不能使用“我”目标( me()方法),因为这是客户端凭证流,所以没有登录用户。 I need to specify the sender in this way: /users/{id | userPrincipalName}/sendMail我需要以这种方式指定发件人: /users/{id | userPrincipalName}/sendMail /users/{id | userPrincipalName}/sendMail . /users/{id | userPrincipalName}/sendMail

This is how the call in Postman is composed: Postman 中的调用是这样构成的:

Method: POST方法:邮寄

URL: URL:

  • .https://graph.microsoft.com/v1.0/users/ {my user id or my user principal name} /sendMail .https://graph.microsoft.com/v1.0/users/ {my user id or my user principal name} /sendMail

AUTHORIZATION:授权:

  • Bearer Token (my access token)不记名令牌(我的访问令牌)

HEADERS:标头:

  • Content-Type: application/json内容类型:application/json

BODY(JSON):身体(JSON):

{
    "recipient":    <recipient email>,
    "subject":      "This is a test mail",
    "from":         <mail that created the tenant, app and access token>,
    "text":         "This is the messge body..."
}

That's the response from the server (in both Postman and Java app):这是来自服务器的响应(在 Postman 和 Java 应用程序中):

STATUS: 405: Method Not Allowed状态: 405:方法不允许

BODY:身体:

{
    "error": {
        "code": "Request_BadRequest",
        "message": "Specified HTTP method is not allowed for the request target.",
        "innerError": {
            "date": "2022-08-05T07:17:34",
            "request-id": "XXXXXXXX-6075-4d13-83ed-XXXXXXXXXXXX",
            "client-request-id": "XXXXXXXX-6075-4d13-83ed-XXXXXXXXXXXX"
        }
    }
}

Note 1: I got my user id and my user principal name also with a Postman call using my access token, exploiting the User.Read.All permission, with the following call:注意 1:我还使用我的访问令牌通过 Postman 调用获得了我的用户 ID 和我的用户主体名称,利用了User.Read.All权限,调用如下:

https://graph.microsoft.com/v1.0/users https://graph.microsoft.com/v1.0/users

That's the response:那是回应:

...
            "userPrincipalName": "XXXXXX_XXXXXX.XXX#EXT#@sendClientCredMail.onmicrosoft.com",
            "id": "XXXXXXXX-4457-4944-bb22-XXXXXXXXXXXX"
        }
    ]
}

Note 2: Note that if I use the "principal name" in the call, I get a 405, if I use the "id" instead I get a 404: Not Found注意 2:请注意,如果我在通话中使用“主体名称”,则会收到 405,如果我使用“id”,则会收到404:未找到

Thanks in advance for any help, I've been trying for hours!在此先感谢您的帮助,我已经尝试了几个小时!

SOLVED解决了

As Optimal said in the comments, the issue was that the user didn't have a licensed mailbox.正如 Optimal 在评论中所说,问题在于用户没有获得许可的邮箱。 When I assigned a "Microsoft 365 Business Premium" license to that user, my code worked fine.当我向该用户分配“Microsoft 365 商业高级版”许可证时,我的代码运行良好。 Check the licenses both on portal.azure.com for assignments, and on admin.microsoft.com for the active mailbox在 portal.azure.com 上检查分配的许可证,在 admin.microsoft.com 上检查活动邮箱的许可证

Kind regards亲切的问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM