![](/img/trans.png)
[英]How do I create an Access Token from Service Account Credentials using REST API?
[英]Create service account credentials for gmail API
我已經閱讀了這個, 這個(PHP 但為了獲得任何幫助), 這個, 這個然后是這里已棄用的相關更改以及對最后一個指向 github 的回復,其中已經解釋了憑據過程。 但是我沒有成功讓示例代碼在這里工作。
我采取的步驟:
錯誤“代碼 400,FAILED_PRECONDITION 持續存在。
兩個具體問題:
代碼在這里:
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();
userEmail
模擬的用戶。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.