pma
pma

Reputation: 35

GMail API Server to Server Authentication

i am trying to implement a task by which to access GMail through the Google API (Java client). The scope of the task is that it will retrieve a user's emails, labels etc. It's my first time interacting with the Google Cloud Platform and i am trying to read through the documentation. Up to now i have fully understood about the "OAuth 2.0 for Server to Server Applications" and i have created a new service account. I am trying to use the tutorials from Google but the classes mentioned in these are deprecated.

My question is, is there any up to date documentation or code example online? Can anyone please provide any pointers on this?

Best regards and thank you so much in advance.

Upvotes: 1

Views: 1053

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116868

I recommend following the official tutorial. If you follow this tutorial just swap out admin sdk for gmail scopes when you configure the service account in your workspace domain. The code will be the same it just needs to be configured for a different api.

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.services.admin.directory.Directory;
import com.google.api.services.admin.directory.DirectoryScopes;
...

/** Email of the Service Account */
private static final String SERVICE_ACCOUNT_EMAIL = "<some-id>@developer.gserviceaccount.com";

/** Path to the Service Account's Private Key file */
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH = "/path/to/<public_key_fingerprint>-privatekey.p12";

/**
 * Build and returns a Directory service object authorized with the service accounts
 * that act on behalf of the given user.
 *
 * @param userEmail The email of the user. Needs permissions to access the Admin APIs.
 * @return Directory service object that is ready to make requests.
 */
public static Directory getDirectoryService(String userEmail) throws GeneralSecurityException,
    IOException, URISyntaxException {
  HttpTransport httpTransport = new NetHttpTransport();
  JacksonFactory jsonFactory = new JacksonFactory();
  GoogleCredential credential = new GoogleCredential.Builder()
      .setTransport(httpTransport)
      .setJsonFactory(jsonFactory)
      .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
      .setServiceAccountScopes(DirectoryScopes.ADMIN_DIRECTORY_USERS)
      .setServiceAccountUser(userEmail)
      .setServiceAccountPrivateKeyFromP12File(
          new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
      .build();
  Directory service = new Directory.Builder(httpTransport, jsonFactory, null)
      .setHttpRequestInitializer(credential).build();
  return service;
}

That sample should be up to date. If its not you should flag it and post a message on the client library issue forum so that they know it needs to be updated Google api java clinet

Upvotes: 1

Related Questions