为 gmail API 创建服务帐户凭据
Create service account credentials for gmail API
我已经阅读, (PHP but to get any help), this, this and then the deprecated related changes 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工作。
我采取的步骤:
错误“代码 400,FAILED_PRECONDITION 仍然存在。
- 然后我将全域授权添加到整个 GP 项目的主服务帐户,下载了这些凭据(使用 .json 文件)但没有成功
两个具体问题:
- 在凭据创建期间我在哪里添加发件人的电子邮件地址 - 我认为,以我有限的知识,实际的电子邮件(GCP 项目所有者电子邮件)需要添加到某个地方。当然,它在创建消息调用期间使用,但也需要将其添加到凭证过程中。
- 同样,我在哪里添加 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);
}
}
代码的其余部分是 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
。
- GCP 项目的所有者/服务帐户的创建者不相关,不需要指定,每个 Service Account User 具有相应权限的人都可以在他的域中使用服务帐户。
我已经阅读
我采取的步骤:
错误“代码 400,FAILED_PRECONDITION 仍然存在。
- 然后我将全域授权添加到整个 GP 项目的主服务帐户,下载了这些凭据(使用 .json 文件)但没有成功
两个具体问题:
- 在凭据创建期间我在哪里添加发件人的电子邮件地址 - 我认为,以我有限的知识,实际的电子邮件(GCP 项目所有者电子邮件)需要添加到某个地方。当然,它在创建消息调用期间使用,但也需要将其添加到凭证过程中。
- 同样,我在哪里添加 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);
}
}
代码的其余部分是 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
。 - GCP 项目的所有者/服务帐户的创建者不相关,不需要指定,每个 Service Account User 具有相应权限的人都可以在他的域中使用服务帐户。