Gmail Java API使用GAE服务帐户

时间:2014-10-17 17:37:59

标签: java google-app-engine google-oauth gmail-api service-accounts

我正在尝试使用GAE服务帐户阅读gmail邮件。类似于下面的代码很适合阅读谷歌日历但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 个答案:

答案 0 :(得分:1)

我能够让它发挥作用。

两件事:

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