![](/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.