Class GoogleCredential (2.5.1)

public class GoogleCredential extends Credential

Deprecated. Please use google-auth-library for handling Application Default Credentials and other non-OAuth2 based authentication.

Thread-safe Google-specific implementation of the OAuth 2.0 helper for accessing protected resources using an access token, as well as optionally refreshing the access token when it expires using a refresh token.

There are three modes supported: access token only, refresh token flow, and service account flow (with or without impersonating a user).

If all you have is an access token, you simply pass the TokenResponse to the credential using Builder#setFromTokenResponse(TokenResponse). Google credential uses BearerToken#authorizationHeaderAccessMethod() as the access method. Sample usage:


 public static GoogleCredential createCredentialWithAccessTokenOnly(TokenResponse tokenResponse) {
   return new GoogleCredential().setFromTokenResponse(tokenResponse);
 }
 

If you have a refresh token, it is similar to the case of access token only, but you additionally need to pass the credential the client secrets using Builder#setClientSecrets(GoogleClientSecrets) or Builder#setClientSecrets(String, String). Google credential uses GoogleOAuthConstants#TOKEN_SERVER_URL as the token server URL, and ClientParametersAuthentication with the client ID and secret as the client authentication. Sample usage:


 public static GoogleCredential createCredentialWithRefreshToken(
     HttpTransport transport, JsonFactory jsonFactory,
     GoogleClientSecrets clientSecrets, TokenResponse tokenResponse) {
  return new GoogleCredential.Builder().setTransport(transport)
                        .setJsonFactory(jsonFactory)
                        .setClientSecrets(clientSecrets)
                        .build()
                        .setFromTokenResponse(tokenResponse);
 }
 

The service account flow is used when you want to access data owned by your client application. You download the private key in a .p12 file from the Google APIs Console. Use Builder#setServiceAccountId(String), Builder#setServiceAccountPrivateKeyFromP12File(File), and Builder#setServiceAccountScopes(Collection). Sample usage:


 public static GoogleCredential createCredentialForServiceAccount(HttpTransport transport,
     JsonFactory jsonFactory,
     String serviceAccountId, Collection<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 Builder#setServiceAccountUser(String). Sample usage:


 public static GoogleCredential createCredentialForServiceAccountImpersonateUser
     (HttpTransport transport, JsonFactory jsonFactory, String serviceAccountId,
      Collection<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 DataStoreFactory and Builder#addRefreshListener(CredentialRefreshListener) with DataStoreCredentialRefreshListener.

If you have a custom request initializer, request execute interceptor, or unsuccessful response handler, take a look at the sample usage for HttpExecuteInterceptor and HttpUnsuccessfulResponseHandler, which are interfaces that this class also implements.

Inheritance

java.lang.Object > com.google.api.client.auth.oauth2.Credential > GoogleCredential

Static Methods

fromStream(InputStream credentialStream) (deprecated)

public static GoogleCredential fromStream(InputStream credentialStream)

Beta
Return a credential defined by a Json file.

Parameter
Name Description
credentialStream InputStream

the stream with the credential definition.

Returns
Type Description
GoogleCredential

the credential defined by the credentialStream.

Exceptions
Type Description
IOException

if the credential cannot be created from the stream.

fromStream(InputStream credentialStream, HttpTransport transport, JsonFactory jsonFactory) (deprecated)

public static GoogleCredential fromStream(InputStream credentialStream, HttpTransport transport, JsonFactory jsonFactory)

Beta
Return a credential defined by a Json file.

Parameters
Name Description
credentialStream InputStream

the stream with the credential definition.

transport com.google.api.client.http.HttpTransport

the transport for Http calls.

jsonFactory com.google.api.client.json.JsonFactory

the factory for Json parsing and formatting.

Returns
Type Description
GoogleCredential

the credential defined by the credentialStream.

Exceptions
Type Description
IOException

if the credential cannot be created from the stream.

getApplicationDefault() (deprecated)

public static GoogleCredential getApplicationDefault()

Beta
Returns the Application Default Credentials.

Returns the Application Default Credentials which are credentials that identify and authorize the whole application. This is the built-in service account if running on Google Compute Engine or the credentials file from the path in the environment variable GOOGLE_APPLICATION_CREDENTIALS.

Returns
Type Description
GoogleCredential

the credential instance.

Exceptions
Type Description
IOException

if the credential cannot be created in the current environment.

getApplicationDefault(HttpTransport transport, JsonFactory jsonFactory) (deprecated)

public static GoogleCredential getApplicationDefault(HttpTransport transport, JsonFactory jsonFactory)

Beta
Returns the Application Default Credentials.

Returns the Application Default Credentials which are credentials that identify and authorize the whole application. This is the built-in service account if running on Google Compute Engine or the credentials file from the path in the environment variable GOOGLE_APPLICATION_CREDENTIALS.

Parameters
Name Description
transport com.google.api.client.http.HttpTransport

the transport for Http calls.

jsonFactory com.google.api.client.json.JsonFactory

the factory for Json parsing and formatting.

Returns
Type Description
GoogleCredential

the credential instance.

Exceptions
Type Description
IOException

if the credential cannot be created in the current environment.

Constructors

GoogleCredential() (deprecated)

public GoogleCredential()

Constructor with the ability to access protected resources, but not refresh tokens.

To use with the ability to refresh tokens, use Builder.

GoogleCredential(GoogleCredential.Builder builder) (deprecated)

protected GoogleCredential(GoogleCredential.Builder builder)
Parameter
Name Description
builder GoogleCredential.Builder

Google credential builder

Methods

createDelegated(String user) (deprecated)

public GoogleCredential createDelegated(String user)

Beta
For service accounts that need to delegate to a specific user, create a copy of the credential with the specified user.

Parameter
Name Description
user String
Returns
Type Description
GoogleCredential

createScoped(Collection<String> scopes) (deprecated)

public GoogleCredential createScoped(Collection<String> scopes)

Beta
For credentials that require scopes, creates a copy of the credential with the specified scopes.

Parameter
Name Description
scopes Collection<String>
Returns
Type Description
GoogleCredential

createScopedRequired() (deprecated)

public boolean createScopedRequired()

Beta
Indicates whether the credential requires scopes to be specified by calling createScoped before use.

Returns
Type Description
boolean

executeRefreshToken() (deprecated)

protected TokenResponse executeRefreshToken()
Returns
Type Description
com.google.api.client.auth.oauth2.TokenResponse
Overrides
com.google.api.client.auth.oauth2.Credential.executeRefreshToken()
Exceptions
Type Description
IOException

getServiceAccountId() (deprecated)

public final String getServiceAccountId()

Returns the service account ID (typically an e-mail address) or null if not using the service account flow.

Returns
Type Description
String

getServiceAccountPrivateKey() (deprecated)

public final PrivateKey getServiceAccountPrivateKey()

Returns the private key to use with the service account flow or null if not using the service account flow.

Returns
Type Description
PrivateKey

getServiceAccountPrivateKeyId() (deprecated)

public final String getServiceAccountPrivateKeyId()

Beta
Returns the ID of the private key to use with the service account flow or null if not using the service account flow.

Returns
Type Description
String

getServiceAccountProjectId() (deprecated)

public final String getServiceAccountProjectId()

Returns the service account Project ID or null if not present, either because this is not using the service account flow, or is using an older version of the service account configuration.

Returns
Type Description
String

getServiceAccountScopes() (deprecated)

public final Collection<String> getServiceAccountScopes()

Returns a collection of OAuth scopes to use with the service account flow or null if not using the service account flow.

Returns
Type Description
Collection<String>

getServiceAccountScopesAsString() (deprecated)

public final String getServiceAccountScopesAsString()

Returns the space-separated OAuth scopes to use with the service account flow or null if not using the service account flow.

Returns
Type Description
String

getServiceAccountUser() (deprecated)

public final String getServiceAccountUser()

Returns the email address of the user the application is trying to impersonate in the service account flow or null for none or if not using the service account flow.

Returns
Type Description
String

setAccessToken(String accessToken) (deprecated)

public GoogleCredential setAccessToken(String accessToken)
Parameter
Name Description
accessToken String
Returns
Type Description
GoogleCredential
Overrides
com.google.api.client.auth.oauth2.Credential.setAccessToken(java.lang.String)

setExpirationTimeMilliseconds(Long expirationTimeMilliseconds) (deprecated)

public GoogleCredential setExpirationTimeMilliseconds(Long expirationTimeMilliseconds)
Parameter
Name Description
expirationTimeMilliseconds Long
Returns
Type Description
GoogleCredential
Overrides
com.google.api.client.auth.oauth2.Credential.setExpirationTimeMilliseconds(java.lang.Long)

setExpiresInSeconds(Long expiresIn) (deprecated)

public GoogleCredential setExpiresInSeconds(Long expiresIn)
Parameter
Name Description
expiresIn Long
Returns
Type Description
GoogleCredential
Overrides
com.google.api.client.auth.oauth2.Credential.setExpiresInSeconds(java.lang.Long)

setFromTokenResponse(TokenResponse tokenResponse) (deprecated)

public GoogleCredential setFromTokenResponse(TokenResponse tokenResponse)
Parameter
Name Description
tokenResponse com.google.api.client.auth.oauth2.TokenResponse
Returns
Type Description
GoogleCredential
Overrides
com.google.api.client.auth.oauth2.Credential.setFromTokenResponse(com.google.api.client.auth.oauth2.TokenResponse)

setRefreshToken(String refreshToken) (deprecated)

public GoogleCredential setRefreshToken(String refreshToken)
Parameter
Name Description
refreshToken String
Returns
Type Description
GoogleCredential
Overrides
com.google.api.client.auth.oauth2.Credential.setRefreshToken(java.lang.String)

toBuilder() (deprecated)

public GoogleCredential.Builder toBuilder()

Beta
Create a builder from this credential.

Returns
Type Description
GoogleCredential.Builder