opers.google.com/accounts/docs/OAuth2ServiceAccount">service account flow is used when you want to access data owned by your client application. You download the private key in a {@code .p12} file from the Google APIs Console. Use{@link Builder#setServiceAccountId(String)}, {@link Builder#setServiceAccountPrivateKeyFromP12File(File)}, and {@link Builder#setServiceAccountScopes(String)}. Sample usage:
public static GoogleCredential createCredentialForServiceAccount( HttpTransport transport, JsonFactory jsonFactory, String serviceAccountId, Iterable<String> serviceAccountScopes, File p12File) throws GeneralSecurityException, IOException { return new GoogleCredential.Builder().setTransport(transport) .setJsonFactory(jsonFactory) .setServiceAccountId(serviceAccountId) .setServiceAccountScopes(serviceAccountScopes) .setServiceAccountPrivateKeyFromP12File(p12File) .build(); }
You can also use the service account flow to impersonate a user in a domain that you own. This is very similar to the service account flow above, but you additionally call {@link Builder#setServiceAccountUser(String)}. Sample usage:
public static GoogleCredential createCredentialForServiceAccountImpersonateUser( HttpTransport transport, JsonFactory jsonFactory, String serviceAccountId, Iterable<String> serviceAccountScopes, File p12File, String serviceAccountUser) throws GeneralSecurityException, IOException { return new GoogleCredential.Builder().setTransport(transport) .setJsonFactory(jsonFactory) .setServiceAccountId(serviceAccountId) .setServiceAccountScopes(serviceAccountScopes) .setServiceAccountPrivateKeyFromP12File(p12File) .setServiceAccountUser(serviceAccountUser) .build(); }
If you need to persist the access token in a data store, use {@link CredentialStore} and{@link Builder#addRefreshListener(CredentialRefreshListener)}.
If you have a custom request initializer, request execute interceptor, or unsuccessful response handler, take a look at the sample usage for {@link HttpExecuteInterceptor} and{@link HttpUnsuccessfulResponseHandler}, which are interfaces that this class also implements.
@since 1.7
@author Yaniv Inbar