gpt4 book ai didi

java - 使用 GAE 服务帐户的 Gmail Java API

转载 作者:太空宇宙 更新时间:2023-11-04 06:28:46 24 4
gpt4 key购买 nike

我正在尝试使用 GAE 服务帐户阅读 Gmail 消息。类似于下面的代码可以很好地读取 Google 日历,但无法读取 Gmail。我在域中只有一名用户(也是管理员帐户)。我正在尝试访问该用户的 Gmail)。任何有关我的代码失败原因的帮助都会很棒。

我已委派以下范围的域范围权限。

Email (Read/Write/Send)  https://mail.google.com/ 
https://www.googleapis.com/auth/admin.directory.user.readonly
Calendar (Read-Write) https://www.googleapis.com/auth/calendar
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/userinfo.profile

我已在 Google App Engine 中启用以下 API:

Admin SDK
Calendar API
Drive API
Gmail API
Google Cloud SQL
Google Cloud Storage
Google Cloud Storage JSON API

下面是创建 gmail 服务的代码:

    // Create HTTP transport
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();


// Create JsonFactory using com.google.api.client.json.jackson2.JacksonFactory
JsonFactory jsonFactory = new JacksonFactory();

// Get service accont (contain service account email and P12 file name)
BaseGoogleService.GoogleServiceAccount serviceAccount = getGoogleServiceAccount();

//load private key from class path to File object.
Resource resource = appContext.getResource("classpath:"+serviceAccount.getServiceAccountPrivateKeyFile());
File privateKeyFile = resource.getFile();

// Create Google credential for service account
List<String> accountScopes = new ArrayList<String>();
accountScopes.add(GmailScopes.MAIL_GOOGLE_COM);
Credential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(serviceAccount.getServiceAccountId())
.setServiceAccountScopes(accountScopes)
.setServiceAccountPrivateKeyFromP12File(privateKeyFile)
.build();

// Create Gmail client

Gmail gmailClient = new Gmail.Builder(httpTransport, jsonFactory, credential)
.build();

读取 Gmail 消息的代码:

ListMessagesResponse response = gmailClient.users().messages().list(userEmail).setQ(query).execute()

执行此代码时出现以下错误

com.google.api.client.googleapis.json.GoogleJsonResponseException: 500 OK
{
"code" : 500,
"errors" : [ {
"domain" : "global",
"message" : "Backend Error",
"reason" : "backendError"
} ],
"message" : "Backend Error"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)

最佳答案

我能够让它发挥作用。

有两件事:

  1. 必须添加".setServiceAccountUser(userEmail)"在构建 GoogleCredential 时。 userEmail 代表要模拟的电子邮件 ID。
  2. 管理员帐户无法执行此操作。我必须将用户添加到域并访问该用户的 Gmail。

关于java - 使用 GAE 服务帐户的 Gmail Java API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26430475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com