簡體   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