com.google.api.services.admin.directory.DirectoryScopes Java Examples

The following examples show how to use com.google.api.services.admin.directory.DirectoryScopes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GoogleProvisioningConnector.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Build and returns a Directory service object authorized with the service accounts that act on
 * behalf of the given user.
 *
 * @return Directory service object that is ready to make requests.
 * @throws IdentityProvisioningException
 */
protected Directory getDirectoryService() throws IdentityProvisioningException {
    boolean isDebugEnabled = log.isDebugEnabled();
    if (isDebugEnabled) {
        log.debug("Starting getDirectoryService() of " + GoogleProvisioningConnector.class);
    }

    String serviceAccountEmailKey = "google_prov_service_acc_email";
    String adminEmailKey = "google_prov_admin_email";
    String privateKeyKey = "google_prov_private_key";
    String applicationNameKey = "google_prov_application_name";

    /** Email of the Service Account */
    String serviceAccountId = this.configHolder.getValue(serviceAccountEmailKey);
    /** Admin email */
    String serviceAccountUser = this.configHolder.getValue(adminEmailKey);
    /** Path to the Service Account's Private Key file */
    String serviceAccountPrivateKeyString = this.configHolder.getValue(privateKeyKey);
    /** Application name */
    String applicationName = this.configHolder.getValue(applicationNameKey);

    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();

    if (isDebugEnabled) {
        log.debug("serviceAccountId" + serviceAccountId);
        log.debug("setServiceAccountScopes"
                  + Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER));
        log.debug("setServiceAccountUser" + serviceAccountUser);
    }

    Directory service = null;
    try {
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport).setJsonFactory(jsonFactory)
                .setServiceAccountId(serviceAccountId)
                .setServiceAccountScopes(Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER))
                .setServiceAccountUser(serviceAccountUser)
                .setServiceAccountPrivateKeyFromP12File(googlePrvKey).build();

        service = new Directory.Builder(httpTransport, jsonFactory, credential)
                .setHttpRequestInitializer(credential).setApplicationName(applicationName)
                .build();

    } catch (GeneralSecurityException | IOException e) {
        throw new IdentityProvisioningException("Error while obtaining connection from google",
                                                e);
    }

    if (log.isDebugEnabled()) {
        log.debug("Ending getDirectoryService() of " + GoogleProvisioningConnector.class);
    }
    return service;
}
 
Example #2
Source File: GoogleClient.java    From account-provisioning-for-google-apps with Apache License 2.0 3 votes vote down vote up
/**
 * Retrieves the service account credentials.
 *
 * @param serviceAccountEmail Service email from the Google Developer Console
 *        project.
 * @param keyPath Where the p12 file is stored.
 * @return GoogleCredential object with the active section.
 * @throws GeneralSecurityException
 * @throws IOException
 */
protected GoogleCredential getCredentialForServiceAccount(String serviceAccountEmail,
    String keyPath) throws GeneralSecurityException, IOException {
  return new GoogleCredential.Builder().setTransport(httpTransport).setJsonFactory(jsonFactory)
      .setServiceAccountId(serviceAccountEmail)
      .setServiceAccountScopes(Collections.singleton(DirectoryScopes.ADMIN_DIRECTORY_USER))
      .setServiceAccountPrivateKeyFromP12File(new File(keyPath)).setServiceAccountUser(authUser)
      .build();
}