.setServiceAccountId("61432502161-bgq2r6vtdefgqsco0i5pjrrpfe8f0dep@developer.gserviceaccount.com") .setServiceAccountScopes(scopes) .setServiceAccountUser("administrator@tremolosecurity-test.com") .setServiceAccountPrivateKeyFromP12File(new File("/Users/mlb/Downloads/TestMyVD-0cf017d369c9.p12")) .build();
.setServiceAccountId(serviceAccountId) .setServiceAccountScopes(Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER)) .setServiceAccountUser(serviceAccountUser) .setServiceAccountPrivateKeyFromP12File(googlePrvKey).build();
.setServiceAccountId(serviceAccountId) .setServiceAccountScopes(Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER)) .setServiceAccountUser(serviceAccountUser) .setServiceAccountPrivateKeyFromP12File(googlePrvKey).build();
.setServiceAccountId(credential.getServiceAccountId()) .setServiceAccountScopes(ImmutableSet.of(ADMIN_DIRECTORY_GROUP_MEMBER_READONLY)) .setServiceAccountUser(gsuiteUserEmail) .setServiceAccountPrivateKey(credential.getServiceAccountPrivateKey()) .build();
.setJsonFactory( getJsonFactory() ) .setServiceAccountScopes( scopes ) .setServiceAccountUser( userAccount ) .build(); .setServiceAccountScopes( scopes ) .setServiceAccountPrivateKeyFromP12File( new File( resource.getPath() ) ) .setServiceAccountUser( userAccount ) .build();
.setTransport(credential.getTransport()) .setServiceAccountUser(this.serviceAccountUser) .build(); } catch (IOException e) {
/** * Creates the thread-safe Google-specific implementation of the OAuth 2.0. * * @param scopes the space-separated OAuth scopes to use with the the service account flow * or {@code null} for none. * @param userAccount the email address. If you want to impersonate a user account, specify the email address. * Useful for domain-wide delegation. * @return the thread-safe credential instance * @throws GeneralSecurityException, IOException */ public HttpRequestInitializer authorize( Collection<String> scopes, String userAccount ) throws GeneralSecurityException, IOException { if ( serviceAccountEmail == null ) { throw new NullPointerException(); } // p12 file load right before usage URL resource = getServiceAccountPrivateKeyP12Resource(); return new ConfiguredGoogleCredential().setTransport( getHttpTransport() ) .setJsonFactory( getJsonFactory() ) .setServiceAccountId( serviceAccountEmail ) .setServiceAccountScopes( scopes ) .setServiceAccountPrivateKeyFromP12File( new File( resource.getPath() ) ) .setServiceAccountUser( userAccount ) .build(); }
.setTransport(credential.getTransport()) .setServiceAccountUser(this.serviceAccountUser) .build(); } catch (IOException e) {
/** Create a new GoogleCredentialWithRetry from a GoogleCredential. */ public static GoogleCredentialWithRetry fromGoogleCredential(GoogleCredential credential) { GoogleCredential.Builder builder = new GoogleCredential.Builder() .setServiceAccountPrivateKey(credential.getServiceAccountPrivateKey()) .setServiceAccountPrivateKeyId(credential.getServiceAccountPrivateKeyId()) .setServiceAccountId(credential.getServiceAccountId()) .setServiceAccountUser(credential.getServiceAccountUser()) .setServiceAccountScopes(credential.getServiceAccountScopes()) .setTokenServerEncodedUrl(credential.getTokenServerEncodedUrl()) .setTransport(credential.getTransport()) .setClientAuthentication(credential.getClientAuthentication()) .setJsonFactory(credential.getJsonFactory()) .setClock(credential.getClock()) .setRequestInitializer(new CredentialHttpRetryInitializer()); GoogleCredentialWithRetry withRetry = new GoogleCredentialWithRetry(builder); // Setting a refresh token requires validation even if it is null. if (credential.getRefreshToken() != null) { withRetry.setRefreshToken(credential.getRefreshToken()); } return withRetry; }
/** Create a new GoogleCredentialWithRetry from a GoogleCredential. */ public static GoogleCredentialWithRetry fromGoogleCredential(GoogleCredential credential) { GoogleCredential.Builder builder = new GoogleCredential.Builder() .setServiceAccountPrivateKey(credential.getServiceAccountPrivateKey()) .setServiceAccountPrivateKeyId(credential.getServiceAccountPrivateKeyId()) .setServiceAccountId(credential.getServiceAccountId()) .setServiceAccountUser(credential.getServiceAccountUser()) .setServiceAccountScopes(credential.getServiceAccountScopes()) .setTokenServerEncodedUrl(credential.getTokenServerEncodedUrl()) .setTransport(credential.getTransport()) .setClientAuthentication(credential.getClientAuthentication()) .setJsonFactory(credential.getJsonFactory()) .setClock(credential.getClock()) .setRequestInitializer(new CredentialHttpRetryInitializer()); GoogleCredentialWithRetry withRetry = new GoogleCredentialWithRetry(builder); // Setting a refresh token requires validation even if it is null. if (credential.getRefreshToken() != null) { withRetry.setRefreshToken(credential.getRefreshToken()); } return withRetry; }
/** * {@link Beta} <br/> * Create a builder from this credential. */ @Beta public Builder toBuilder() { Builder builder = new GoogleCredential.Builder() .setServiceAccountPrivateKey(serviceAccountPrivateKey) .setServiceAccountPrivateKeyId(serviceAccountPrivateKeyId) .setServiceAccountId(serviceAccountId) .setServiceAccountProjectId(serviceAccountProjectId) .setServiceAccountUser(serviceAccountUser) .setServiceAccountScopes(serviceAccountScopes) .setTokenServerEncodedUrl(getTokenServerEncodedUrl()) .setTransport(getTransport()) .setJsonFactory(getJsonFactory()) .setClock(getClock()); builder.setClientAuthentication(getClientAuthentication()); return builder; }
private Credential authorizeServiceAccount(String emailAddress, String p12FileName, Collection<String> scopes, String user) throws Exception { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); // set the service account user when provided GoogleCredential credential = new GoogleCredential.Builder() .setTransport(httpTransport) .setJsonFactory(jsonFactory) .setServiceAccountId(emailAddress) .setServiceAccountPrivateKeyFromP12File(new File(p12FileName)) .setServiceAccountScopes(scopes) .setServiceAccountUser(user) .build(); return credential; } }
/** * {@link Beta} <br/> * For service accounts that need to delegate to a specific user, create a * copy of the credential with the specified user. */ @Beta public GoogleCredential createDelegated(String user) { if (serviceAccountPrivateKey == null) { return this; } return toBuilder() .setServiceAccountUser(user) .build(); }