繁体   English   中英

为 gmail API 创建服务帐户凭据

[英]Create service account credentials for gmail API

我已经阅读了这个这个(PHP 但为了获得任何帮助), 这个这个然后是这里已弃用的相关更改以及对最后一个指向 github 的回复,其中已经解释了凭据过程。 但是我没有成功让示例代码在这里工作。

我采取的步骤:

  1. 已经在进行 GCP 云项目,需要将这个 email 作为服务帐户发送出去。
  2. 已经创建了一个额外的服务帐户来处理 email API 并通过向此处的服务帐户添加 API 访问和全域委托来授权它。

错误“代码 400,FAILED_PRECONDITION 持续存在。

  1. 然后我将全域授权添加到整个 GP 项目的主要服务帐户,下载了这些凭据(使用 .json 文件)并且没有运气

两个具体问题:

  1. 在凭据创建过程中,我在哪里添加发件人的 email 地址 - 我认为,以我有限的知识,需要在某处添加实际的 email(GCP 项目所有者电子邮件)。 当然,它在创建消息调用期间使用,但也需要将其添加到凭证过程中。
  2. 同样,我在哪里添加 serviceAccountUserEmail - 它在已弃用的 GoogleCredential 中,但在 GoogleCredentials 中不存在(新的 class 以“s”结尾)。

代码在这里:

public class Main {
    private static final String APPLICATION_NAME = "Gmail mail server";
    private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();

    public static void main(String[] args) throws IOException, MessagingException {
        final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("resources/credentials.json"))
                .createScoped(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_MODIFY, GmailScopes.MAIL_GOOGLE_COM);
        HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(creds);
        HttpTransport transport = new NetHttpTransport.Builder().build();
        // Construct the gmail object.
        Gmail gm = new Gmail.Builder(transport, JSON_FACTORY, requestInitializer)
                .setApplicationName(APPLICATION_NAME)
                .build();

        //create the message
        String to = "<a real email id>";      //actual email address used, is masked for privacy
        String from = "<GCP Project Owner email ID>";         //actual email address that owns the GCP project
        String subject = "Test...";
        String bodytext = "\nHello ..  first test email ...";

        MimeMessage an_email = createEmail(to, from, subject, bodytext);
        Message ret_val = sendMessage(gm, "<actual email of GCP Project Owner, not string me", an_email);
    }
}

代码的 rest 是 GCP Gmail API 中提供的内容的实际复制粘贴,因此我不会重新复制它。

欢迎所有指导。 本人Java水平一般,请大家帮忙指导详细一点。

谢谢

要使用GoogleCredentials实现模拟,请使用createDelegated(String user)

final GoogleCredentials creds = ServiceAccountCredentials.fromStream(new FileInputStream("resources/credentials.json"))
     .createScoped(GmailScopes.GMAIL_SEND, GmailScopes.GMAIL_COMPOSE, GmailScopes.GMAIL_MODIFY, GmailScopes.MAIL_GOOGLE_COM);
final GoogleCredentials delegatedCreds = creds.createDelegated(userEmail);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(delegatedCreds);
HttpTransport transport = new NetHttpTransport.Builder().build();
// Construct the gmail object.
Gmail gm = new Gmail.Builder(transport, JSON_FACTORY, requestInitializer)
      .setApplicationName(APPLICATION_NAME)
      .build();

  • 因此, email 的发件人将是服务帐户userEmail模拟的用户。
  • GCP 项目的所有者/服务帐户的创建者是不相关的,不需要指定,每个具有各自权限的服务帐户用户都可以在他的域中使用服务帐户。

暂无
暂无

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

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