为Gmail API创建服务帐户凭据

发布于 2025-01-23 13:40:38 字数 2782 浏览 0 评论 0原文

我读过 this this (php但要获得任何帮助), this this ,然后倒闭的相关更改 there 以及对最后一个指向凭证过程的Github的答复。但是,在获取示例代码这里)上班时,我没有成功。

我采取的步骤是:

  1. 已经在一个GCP云项目上工作,该项目需要作为服务帐户发送此电子邮件。
  2. 创建了一个仅仅用于处理电子邮件API的其他服务帐户,并通过将API访问和域名代表团添加到服务帐户在这里

错误“代码400,Failed_precondition

  1. 存在

持续

  1. 。凭据创建期间,发件人的电子邮件地址 - 我认为,根据我的有限了解,实际上需要在创建消息呼叫期间添加实际电子邮件(GCP项目所有者)。 。
  2. 同样,我在哪里添加

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);
    }
}

凭据 GCP GMAIL API中提供的实际粘贴,因此

在Java中的熟练程度不再重复

I have read this, this (PHP but to get any help), this, this and then the deprecated related changes here and the reply to the last one that points to the github where the credentials process has been explained. But am having no success with getting the sample code here to work.

The steps that I have taken :

  1. Was already working on a GCP cloud project which needs to send out this email, as a service account.
  2. Had created an additional service account just to handle email API and authorized it by adding API access and domainwide delegation to the service account here.

The error "code 400, FAILED_PRECONDITION persisted.

  1. Then I added the domainwide authorization to the main service account for the entire GP project, downloaded those credentials (using the .json file) and have had no luck

Two specific issues :

  1. Where do I add the email address of the sender during credential creation - I think, in my limited knowledge, that the actual email (the GCP Project owner email) needs to be added somewhere. It is used during create message call, of course, but it needs to be added to the credential process as well.
  2. Similarly, where do I add serviceAccountUserEmail - it was there in deprecated GoogleCredential, but not there in GoogleCredentials (the new class ends in 's').

The code is here :

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);
    }
}

The rest of the code is an actual copy paste of what has been provided in the GCP Gmail API so am not reproducing it all over again.

All guidance is welcome. My proficiency in Java is average, so please help with a little more detailed guidance.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2025-01-30 13:40:38

googlecredentials使用 create extere exele exele eleaged(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();

  • 在其中,电子邮件的发送者将是由服务。
  • GCP项目/服务帐户的创建者的所有者无关紧要,不需要指定,每个服务帐户用户使用相应的墨盒可以将服务帐户用于其域。

To implement impersonation with GoogleCredentials use 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();

  • Thereby, the sender of the email will be the user impersonated by the service account userEmail.
  • The owner of the GCP project / creator of the service account is not relevant and does not need to be specified, every Service Account User with the respective permisisons can use the service account for his domain.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文