Gmail API returns "Delegation denied for my.email@email.com" 将不同的电子邮件设置为收件人时

Gmail API returns "Delegation denied for my.email@email.com" when setting a different email as recipient

我正在使用 Gmail API 创建草稿。 当我创建收件人是我自己的电子邮件(生成凭据的电子邮件)的草稿消息时,一切正常,但当我尝试使用不同的电子邮件时,会打印以下消息:

{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Delegation denied for my.email.here@gmail.com",
    "reason" : "forbidden"
  } ],
  "message" : "Delegation denied for my.email.here@gmail.com",
  "status" : "PERMISSION_DENIED"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)

我就是这样组装我的 MimeMessage:

val props = Properties()
val session = Session.getDefaultInstance(props, null)
val message = MimeMessage(session)
message.setFrom(message.sender)
message.addRecipient(JavaxMessage.RecipientType.TO, InternetAddress("different.email.here@gmail.com"))
message.subject = subject

我正在使用的示波器:

// "https://www.googleapis.com/auth/gmail.compose"
GmailScopes.GMAIL_COMPOSE

我已经尝试了很多东西来让它工作,但我没有任何成功。

Delegation denied for my.email.here@gmail.com

这封电子邮件似乎是标准的 gmail 电子邮件地址。您似乎在尝试将服务帐户委派给具有标准 Gmail 电子邮件地址的用户。

服务帐户仅适用于 google 工作区电子邮件帐户。您需要为 serveries 帐户设置域范围的委派,然后您可以设置委派用户。

如果您想使用标准的 gmail 帐户,您需要使用 Oauth2 授权用户。

private fun getCredentials(httpTransport: NetHttpTransport): Credential? {
    val inputStream = File("credentials.json").inputStream()
    val clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, InputStreamReader(inputStream))
    val flow = GoogleAuthorizationCodeFlow.Builder(httpTransport, JSON_FACTORY, clientSecrets, SCOPES)
            .setDataStoreFactory(FileDataStoreFactory(File(TOKENS_DIRECTORY_PATH)))
            .setAccessType("offline")
            .build()
    val receiver = LocalServerReceiver.Builder().setPort(8888).build()
    return AuthorizationCodeInstalledApp(flow, receiver).authorize("user")
}



 public static MimeMessage createEmail(String to,
                                          String from,
                                          String subject,
                                          String bodyText)
            throws MessagingException {
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        MimeMessage email = new MimeMessage(session);

        email.setFrom(new InternetAddress(from));
        email.addRecipient(javax.mail.Message.RecipientType.TO,
                new InternetAddress(to));
        email.setSubject(subject);
        email.setText(bodyText);
        return email;
    }

Sending

感谢@DaImTo 的回复和提供的所有信息。


根据提供的信息,对于那些和我有同样问题的人,我找到了一个很好的解决方法:

  • 将“TO”收件人设置为用户的电子邮件;
  • 将“BCC”收件人添加到实际收件人电子邮件中。

下面是演示代码:

    val meProfile = gmail.users().getProfile("me").execute()
    val toRecipient = InternetAddress(meProfile.emailAddress)
    val ccRecipient = InternetAddress(to)

    val props = Properties()
    val session = Session.getDefaultInstance(props, null)
    val message = MimeMessage(session)
    message.setFrom(message.sender)

    // here's the magic :D
    message.addRecipient(JavaxMessage.RecipientType.TO, toRecipient)
    message.addRecipient(JavaxMessage.RecipientType.BCC, ccRecipient)

    message.subject = subject