google app engine - Gmail Java API Using GAE Service account -
I am trying to read Gmail messages using a GAe service account. The code below works well to read Google Calendar but fails for Gmail I have only one user in the domain (which is also an administrator account). I'm trying to reach that user's 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/aprofit/https://www.googleapis. Com / auth / userinfo.email https://www.googleapis.com/auth/userinfo.profile
i enabled in the Google app engine below the API:
admin SDK Calendar API drive API Gmail API Google Cloud SQL Google Cloud Storage Google Cloud Storage Jesoan API
the code below Gmail Ewa Makes:
// HTTP transport HTTP transport to httpTransport = GoogleNetHttpTransport.newTrustedTransport (); Create JsonFactory using // com.google.api.client.json.jackson2 Jacksonfactor Jasonfattery JasonFactor = New Jacksonfactor (); // Get the service (Include service account email and p12 file name) Base GoogleService. GoogleServiceAccount serviceAccount = getGoogleServiceAccount (); // Load the private key from the path of the path to the file path resource resource = appContext.getResource ("ClassPath:" + service account .getServiceAccountPrivateKeyFile ()); PrivateKeyFile file = resource.getFile (); // Create Google credentials for the service account list & lt; String & gt; AccountScopes = New Arrestists & lt; String & gt; (); AccountScopes.add (GmailScopes.MAIL_GOOGLE_COM); Credential credentials = new GoogleCredential.builder () .setTransport (httpTransport) .setJsonFactory (jsonfactory) .setServiceAccountId (serviceAccount.getServiceAccountId ()) .setServiceAccountScope .setServiceAccountPrivateKeyFromP12File (privateKeyFile) .build (); // Gmail Client Gmail Gmail Client = New Gmail Build Builder (HTTPRARSport, JSNAFactor, Credential) Build ();
The code that reads Gmail messages:
ListMessagesResponse response = gmailClient.users () message () list (USEREMAIL) .setQ (query). .. Execute ()
When I execute the code I am getting below error
com.google.api.client.googleapis.json.GoogleJsonResponseException: 500 The "Message": "Message": "Message": "Backend error": ["code": 500, "Errors": [{"domain": "global", "message": "backend error", "cause": "backed error"}] } on com.google.api.client.googleapis.json.GoogleJsonResponseException.from com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError (AbstractGoogleJsonClientRequest.java:113) (GoogleJsonResponseException.java:145) on com . google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError (AbstractGoogleJsonClientRequest.java:40) com on com.google.api.client.googleapis.services.AbstractGoogleClientRequest $ 1.interceptResponse (AbstractGoogleClientRequ est.java:312). on com.google.api.client.http.HttpRequest.execute (HttpRequest.java:1049) on google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed (AbstractGoogleClientRequest.java:410) com.google.api.client. on googleapis.services.AbstractGoogleClientRequest.executeUnparsed (AbstractGoogleClientRequest.java:343) on com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute (AbstractGoogleClientRequest.java:460)
I was able to work it out.
Two things:
- Adding a GoogleCredential representation represents the UserEmail Email ID to be impersonated.
- This can not be done for the administrator account. I had to add a user to the domain and that user had access to Gmail.
Comments
Post a Comment