6

Is it possible to use Google Credentials for GMAIL REST API without using impersonation??

In many examples of GMAIL REST API i see is mandatory to use an impersonation account associated to a domain and Google APPS. I just want to use GMAIL REST API api server-to-server :

f.e:

GoogleCredential  credential = new GoogleCredential.Builder().setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
                         .setServiceAccountId(serviceAccountUserEmail)
                         **.setServiceAccountUser("myuser@mydomain.com)**
                         .setServiceAccountScopes(SCOPES)
                         .setServiceAccountPrivateKeyFromP12File(
                         new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))                                
                        .build();

The matter is that i don't have any domain and i use just a GMAIL account...but i don't get any way to authorize, for example : MyMainGmailAcccoun@gmail.com

The servive acccount id 4xxxxxxxxxxxxxq@developer.gserviceaccount.com"; is a "client account" created with the MyMainGmailAcccoun@gmail.com

This has no sense , i don't want to impersonate with no other account of another domain, and i don't get any way to authozize MyMainGmailAcccoun@gmail.com .

Just only works, if you have an account to impersonate of other domain associated with Google Apps, and to get Google Apps...you need a domain.

Any advise??

Azimuts
  • 1,212
  • 4
  • 16
  • 35

1 Answers1

4

You may just want to simply use OAuth 2.0 for Web Server Applications. Assuming you want the authorization for more than an hour, you can use refresh tokens.

Jay Lee
  • 13,415
  • 3
  • 28
  • 59
  • 2
    No, i just want to user the Google Mail Service , server-to-server. Just invoking the GMAIL REST Service from a java stand alone application [server side]. When i invoke the service i create a Google Credentials using a p12 file create just for this service. – Azimuts Mar 30 '15 at 10:22
  • 1
    You cannot use service accounts with consumer (non-Google Apps) accounts. You must use the Web Server flow or else the Installed App flow: https://developers.google.com/accounts/docs/OAuth2InstalledApp – Jay Lee Mar 30 '15 at 10:23
  • 1
    It's also still not clear to me why you feel so strongly about using a service account. What can't you do with the web server flow? – Jay Lee Mar 30 '15 at 10:24
  • NO? Neither using a GMail Account user??. I'm using my main Gmail account., but always get .. 401 Unauthorized . I cannot create a Google Apps Acccount because i have no custom domain. No way to do this???? – Azimuts Mar 30 '15 at 10:30
  • 1
    There is no way to do this with a service account. You're effectively saying, how do I put the screw in the board with a hammer? Use the screwdriver... – Jay Lee Mar 30 '15 at 10:32
  • 3
    Azimuts, Jay is right. "Service account" here means you're logging in as an account that's not a real (human) Google account. Such accounts cannot use Gmail, they are only used for services that aren't tied to a specific person (e.g. maps and search APIs, not drive, calendar, gmail, etc). One can use a service account with domain wide delegation to get access to any person in the domain (given the domain administrator whitelisting such access), but that's probably not what you want here (never works for @gmail.com accounts). – Eric D Mar 30 '15 at 15:50
  • Yeah! Thank you. I thought that with the clientID and the developer account was enought. With the domain wide delegation there is no problem. Thank you. – Azimuts Mar 31 '15 at 08:17
  • So if this service account cannot be used with any regular gmail account then how to pull the contacts and email for a regular gmail account from a server side program (without any user input)? – Rahatur Feb 17 '16 at 11:06