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(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
GoogleCredential.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
GoogleCredential.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.
| 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(GoogleCredential.Builder builder) |
| Modifier and Type | Method and Description |
|---|---|
protected com.google.api.client.auth.oauth2.TokenResponse |
executeRefreshToken() |
String |
getServiceAccountId()
Beta Returns the service account ID (typically an e-mail address) or null if not using the
service account flow. |
PrivateKey |
getServiceAccountPrivateKey()
Beta Returns the private key to use with the the service account flow or null if not using
the service account flow. |
Collection<String> |
getServiceAccountScopes()
Beta Returns a collection of OAuth scopes to use with the the service account flow or null
if not using the service account flow. |
String |
getServiceAccountScopesAsString()
Beta 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()
Beta 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, refreshTokenpublic GoogleCredential()
To use with the ability to refresh tokens, use GoogleCredential.Builder.
protected GoogleCredential(GoogleCredential.Builder builder)
builder - Google credential builderpublic GoogleCredential setAccessToken(String accessToken)
setAccessToken in class com.google.api.client.auth.oauth2.Credentialpublic GoogleCredential setRefreshToken(String refreshToken)
setRefreshToken in class com.google.api.client.auth.oauth2.Credentialpublic GoogleCredential setExpirationTimeMilliseconds(Long expirationTimeMilliseconds)
setExpirationTimeMilliseconds in class com.google.api.client.auth.oauth2.Credentialpublic GoogleCredential setExpiresInSeconds(Long expiresIn)
setExpiresInSeconds in class com.google.api.client.auth.oauth2.Credentialpublic GoogleCredential setFromTokenResponse(com.google.api.client.auth.oauth2.TokenResponse tokenResponse)
setFromTokenResponse in class com.google.api.client.auth.oauth2.Credential@Beta
protected com.google.api.client.auth.oauth2.TokenResponse executeRefreshToken()
throws IOException
executeRefreshToken in class com.google.api.client.auth.oauth2.CredentialIOException@Beta public final String getServiceAccountId()
Beta null if not using the
service account flow.@Beta public final Collection<String> getServiceAccountScopes()
Beta null
if not using the service account flow.@Beta public final String getServiceAccountScopesAsString()
Beta null if not using the service account flow.@Beta public final PrivateKey getServiceAccountPrivateKey()
Beta null if not using
the service account flow.@Beta public final String getServiceAccountUser()
Beta null for none or if not using the service account flow.Copyright © 2010-2013 Google. All Rights Reserved.