为Gmail API创建服务帐户凭据
我读过 this , this (php但要获得任何帮助), this , this ,然后倒闭的相关更改 there 以及对最后一个指向凭证过程的Github的答复。但是,在获取示例代码这里)上班时,我没有成功。
我采取的步骤是:
- 已经在一个GCP云项目上工作,该项目需要作为服务帐户发送此电子邮件。
- 创建了一个仅仅用于处理电子邮件API的其他服务帐户,并通过将API访问和域名代表团添加到服务帐户在这里。
错误“代码400,Failed_precondition
- 存在
持续
- 。凭据创建期间,发件人的电子邮件地址 - 我认为,根据我的有限了解,实际上需要在创建消息呼叫期间添加实际电子邮件(GCP项目所有者)。 。
- 同样,我在哪里添加
了
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 :
- Was already working on a GCP cloud project which needs to send out this email, as a service account.
- 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.
- 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 :
- 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.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
用
googlecredentials
使用 create extere exele exele eleaged(string user):To implement impersonation with
GoogleCredentials
use createDelegated(String user):userEmail
.