Gmail Java API Използване на акаунт в услугата GAE

Опитвам се да чета gmail съобщения, използвайки акаунт в услугата GAE. Кодът, подобен на този по-долу, работи добре за четене на 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

Активирах следните API в Google App Engine:

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)

person Srik    schedule 17.10.2014    source източник
comment
Разбрах. Две неща: 1. Трябва да добавите .setServiceAccountUser(userEmail), докато изграждате GoogleCredential. 2. Това не може да се направи за администраторски акаунт. Трябваше да добавя потребител към домейна и да получа достъп до gmail на този потребител.   -  person Srik    schedule 18.10.2014


Отговори (1)


Успях да го накарам да работи.

Две неща:

  1. Трябва да добавите ".setServiceAccountUser(userEmail)" при изграждане на GoogleCredential. userEmail представлява имейл идентификаторът, който да се имитира.
  2. Това не може да се направи за администраторски акаунт. Трябваше да добавя потребител към домейна и да получа достъп до gmail на този потребител.
person Srik    schedule 18.10.2014