1
votes

I am trying to read gmail messages using GAE Service account. The code similar to below works well to read Google calendar but fails for gmail. I have only one user in the domain (who is also the admin account). I am trying to access the gmail of that user). Any help on why my code fails would be great.

I have delegated domain wide authority for the following scopes.

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

I have enabled below APIs in the Google App Engine:

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

Below is the code that creates gmail service:

    // 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();

Code that reads gmail messages:

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

I am getting below error when I execute this code

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
I got it. Two things: 1. Have to add ".setServiceAccountUser(userEmail)" while building GoogleCredential. 2. This cannot be done for admin account. I had to add a user to the domain and access that user's gmail.Srik

1 Answers

2
votes

I was able to make it work.

Two things:

  1. Have to add ".setServiceAccountUser(userEmail)" while building GoogleCredential. userEmail represents the email id to impersonate.
  2. This cannot be done for admin account. I had to add a user to the domain and access that user's gmail.