gmail-api - Google API FAILED_PRECONDITION 前置条件检查失败
问题描述
我目前在调用 Gmail API 时遇到 400 错误。
我在我的 Google Cloud 控制台中启用了 Gmail API(它是 G Suite 帐户,而不是个人帐户)。
我已经创建了一个服务帐户,然后尝试使用邮递员来测试 Gmail API。
我正在使用我的服务帐户来交换承载令牌
我已经要求了所有可能的范围。
[https://www.googleapis.com/auth/gmail.addons.current.message.readonly,https://www.googleapis.com/auth/gmail.addons.current.action.compose,https://www .googleapis.com/auth/gmail.settings.basic, https://www.googleapis.com/auth/gmail.labels, https://www.googleapis.com/auth/gmail.settings.sharing, https:// /www.googleapis.com/auth/gmail.addons.current.message.metadata,https://www.googleapis.com/auth/gmail.readonly,https://www.googleapis.com/auth/gmail.send , https://www.googleapis.com/auth/gmail.addons.current.message.action, https://www.googleapis.com/auth/gmail.compose, https://www.googleapis.com/auth /gmail.insert,https://www.googleapis.com/auth/gmail.metadata,https://mail.google.com/,https://www.googleapis.com/auth/gmail.modify]
然后我使用令牌访问以下端点。
获取https://gmail.googleapis.com/gmail/v1/users/me/messages
它给了我以下错误。我已经在谷歌上搜索了几天,但我没有运气。
{
"error": {
"code": 400,
"message": "Precondition check failed.",
"errors": [
{
"message": "Precondition check failed.",
"domain": "global",
"reason": "failedPrecondition"
}
],
"status": "FAILED_PRECONDITION"
}
}
这是我的代码:
public static void main(String[] args) {
try {
NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredentials credentials = ServiceAccountCredentials
.fromStream(new FileInputStream("/home/rl/.gsutil/prive-production-appDev.json"));
credentials = credentials.createScoped(GmailScopes.all());
System.out.println(credentials.getAuthenticationType());
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
email.setFrom(new InternetAddress("xxxx"));
email.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress("xxxx"));
email.setSubject("test");
email.setText("test");
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
email.writeTo(buffer);
String encodedEmail = Base64.encodeBase64URLSafeString(buffer.toByteArray());
com.google.api.services.gmail.model.Message message = new com.google.api.services.gmail.model.Message();
message.setRaw(encodedEmail);
HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credentials);
Gmail gmail = new Gmail.Builder(httpTransport, JacksonFactory.getDefaultInstance(), requestInitializer)
.setApplicationName("Prive Technologies")
.build();
ListMessagesResponse res = gmail.users().messages().list("me").execute();
message = gmail.users().messages().send("me", message).execute();
} catch (IOException | MessagingException | GeneralSecurityException e) {
e.printStackTrace();
}
}
我正在使用这个:
解决方案
推荐阅读
- java - 如何清空已使用 glide 加载的 Imageview
- php - 有没有办法使用 php 从特定行到文件末尾读取 csv 文件?
- java - 在 Bean 类中使用 ApplicationContextAware 来初始化原型作用域 bean 成员
- node.js - 项目中发生错误时报告错误
- firebase - 从 Firestore 数据库获取数组
- javascript - 如何使脚本自动运行(每天),提交已更改的文件(由脚本)并推送到 GitHub
- nativescript - Nativescript 中的单选按钮
- c# - 如何通过主调用使 ac# 库不可见?
- azure-keyvault - DataProtectionBuilder.ProtectKeysWithAzureKeyVault 在 .net 4.7.2 中不起作用
- linker - 如何在 lli (LLVM) 中使用 I Address Sanitizer