public class GoogleCredential
extends com.google.api.client.auth.oauth2.Credential
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 Credential.setFromTokenResponse(TokenResponse)
. Google credential uses
BearerToken.authorizationHeaderAccessMethod()
as the access method. Sample usage:
public static GoogleCredential createCredentialWithAccessTokenOnly( HttpTransport transport, JsonFactory jsonFactory, 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
GoogleCredential.Builder.setClientSecrets(GoogleClientSecrets)
or
GoogleCredential.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
GoogleCredential.Builder.setServiceAccountId(String)
,
GoogleCredential.Builder.setServiceAccountPrivateKeyFromP12File(File)
, and
GoogleCredential.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
GoogleCredential.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 CredentialStore
and
GoogleCredential.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 HttpExecuteInterceptor
and
HttpUnsuccessfulResponseHandler
, which are interfaces that this class also implements.
Modifier and Type | Class and Description |
---|---|
static class |
GoogleCredential.Builder
Google credential builder.
|
Modifier | Constructor and Description |
---|---|
|
GoogleCredential()
Constructor with the ability to access protected resources, but not refresh tokens.
|
protected |
GoogleCredential(com.google.api.client.auth.oauth2.Credential.AccessMethod method,
com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory,
String tokenServerEncodedUrl,
com.google.api.client.http.HttpExecuteInterceptor clientAuthentication,
com.google.api.client.http.HttpRequestInitializer requestInitializer,
List<com.google.api.client.auth.oauth2.CredentialRefreshListener> refreshListeners,
String serviceAccountId,
String serviceAccountScopes,
PrivateKey serviceAccountPrivateKey,
String serviceAccountUser)
Deprecated.
(scheduled to be removed in 1.15) Use
GoogleCredential(Builder) |
protected |
GoogleCredential(com.google.api.client.auth.oauth2.Credential.AccessMethod method,
com.google.api.client.http.HttpTransport transport,
com.google.api.client.json.JsonFactory jsonFactory,
String tokenServerEncodedUrl,
com.google.api.client.http.HttpExecuteInterceptor clientAuthentication,
com.google.api.client.http.HttpRequestInitializer requestInitializer,
List<com.google.api.client.auth.oauth2.CredentialRefreshListener> refreshListeners,
String serviceAccountId,
String serviceAccountScopes,
PrivateKey serviceAccountPrivateKey,
String serviceAccountUser,
com.google.api.client.util.Clock clock)
Deprecated.
(scheduled to be removed in 1.15) Use
GoogleCredential(Builder) |
protected |
GoogleCredential(GoogleCredential.Builder builder) |
Modifier and Type | Method and Description |
---|---|
protected com.google.api.client.auth.oauth2.TokenResponse |
executeRefreshToken() |
String |
getServiceAccountId()
Returns the service account ID (typically an e-mail address) or
null if not using the
service account flow. |
PrivateKey |
getServiceAccountPrivateKey()
Returns the private key to use with the the service account flow or
null if not using
the service account flow. |
String |
getServiceAccountScopes()
Returns the space-separated OAuth scopes to use with the the service account flow or
null if not using the service account flow. |
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. |
GoogleCredential |
setAccessToken(String accessToken) |
GoogleCredential |
setExpirationTimeMilliseconds(Long expirationTimeMilliseconds) |
GoogleCredential |
setExpiresInSeconds(Long expiresIn) |
GoogleCredential |
setFromTokenResponse(com.google.api.client.auth.oauth2.TokenResponse tokenResponse) |
GoogleCredential |
setRefreshToken(String refreshToken) |
getAccessToken, getClientAuthentication, getClock, getExpirationTimeMilliseconds, getExpiresInSeconds, getJsonFactory, getMethod, getRefreshListeners, getRefreshToken, getRequestInitializer, getTokenServerEncodedUrl, getTransport, handleResponse, initialize, intercept, refreshToken
public GoogleCredential()
To use with the ability to refresh tokens, use GoogleCredential.Builder
.
protected GoogleCredential(GoogleCredential.Builder builder)
builder
- Google credential builder@Deprecated protected GoogleCredential(com.google.api.client.auth.oauth2.Credential.AccessMethod method, com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, String tokenServerEncodedUrl, com.google.api.client.http.HttpExecuteInterceptor clientAuthentication, com.google.api.client.http.HttpRequestInitializer requestInitializer, List<com.google.api.client.auth.oauth2.CredentialRefreshListener> refreshListeners, String serviceAccountId, String serviceAccountScopes, PrivateKey serviceAccountPrivateKey, String serviceAccountUser)
GoogleCredential(Builder)
method
- method of presenting the access token to the resource server (for example
BearerToken.authorizationHeaderAccessMethod()
)transport
- HTTP transport for executing refresh token request or null
if not
refreshing tokensjsonFactory
- JSON factory to use for parsing response for refresh token request or
null
if not refreshing tokenstokenServerEncodedUrl
- encoded token server URL or null
if not refreshing tokensclientAuthentication
- client authentication or null
for none (see
TokenRequest.setClientAuthentication(HttpExecuteInterceptor)
)requestInitializer
- HTTP request initializer for refresh token requests to the token
server or null
for none.refreshListeners
- listeners for refresh token results or null
for noneserviceAccountId
- service account ID (typically an e-mail address) or null
if not
using the service account flowserviceAccountScopes
- space-separated OAuth scopes to use with the the service account
flow or null
if not using the service account flowserviceAccountPrivateKey
- private key to use with the the service account flow or
null
if not using the service account flowserviceAccountUser
- 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@Deprecated protected GoogleCredential(com.google.api.client.auth.oauth2.Credential.AccessMethod method, com.google.api.client.http.HttpTransport transport, com.google.api.client.json.JsonFactory jsonFactory, String tokenServerEncodedUrl, com.google.api.client.http.HttpExecuteInterceptor clientAuthentication, com.google.api.client.http.HttpRequestInitializer requestInitializer, List<com.google.api.client.auth.oauth2.CredentialRefreshListener> refreshListeners, String serviceAccountId, String serviceAccountScopes, PrivateKey serviceAccountPrivateKey, String serviceAccountUser, com.google.api.client.util.Clock clock)
GoogleCredential(Builder)
method
- method of presenting the access token to the resource server (for example
BearerToken.authorizationHeaderAccessMethod()
)transport
- HTTP transport for executing refresh token request or null
if not
refreshing tokensjsonFactory
- JSON factory to use for parsing response for refresh token request or
null
if not refreshing tokenstokenServerEncodedUrl
- encoded token server URL or null
if not refreshing tokensclientAuthentication
- client authentication or null
for none (see
TokenRequest.setClientAuthentication(HttpExecuteInterceptor)
)requestInitializer
- HTTP request initializer for refresh token requests to the token
server or null
for none.refreshListeners
- listeners for refresh token results or null
for noneserviceAccountId
- service account ID (typically an e-mail address) or null
if not
using the service account flowserviceAccountScopes
- space-separated OAuth scopes to use with the the service account
flow or null
if not using the service account flowserviceAccountPrivateKey
- private key to use with the the service account flow or
null
if not using the service account flowserviceAccountUser
- 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
flowclock
- The clock to use for expiration checkpublic GoogleCredential setAccessToken(String accessToken)
setAccessToken
in class com.google.api.client.auth.oauth2.Credential
public GoogleCredential setRefreshToken(String refreshToken)
setRefreshToken
in class com.google.api.client.auth.oauth2.Credential
public GoogleCredential setExpirationTimeMilliseconds(Long expirationTimeMilliseconds)
setExpirationTimeMilliseconds
in class com.google.api.client.auth.oauth2.Credential
public GoogleCredential setExpiresInSeconds(Long expiresIn)
setExpiresInSeconds
in class com.google.api.client.auth.oauth2.Credential
public GoogleCredential setFromTokenResponse(com.google.api.client.auth.oauth2.TokenResponse tokenResponse)
setFromTokenResponse
in class com.google.api.client.auth.oauth2.Credential
protected com.google.api.client.auth.oauth2.TokenResponse executeRefreshToken() throws IOException
executeRefreshToken
in class com.google.api.client.auth.oauth2.Credential
IOException
public final String getServiceAccountId()
null
if not using the
service account flow.public final String getServiceAccountScopes()
null
if not using the service account flow.public final PrivateKey getServiceAccountPrivateKey()
null
if not using
the service account flow.public final String getServiceAccountUser()
null
for none or if not using the service account flow.Copyright © 2010-2013 Google. All Rights Reserved.