Class GoogleAccountCredential (2.5.1)

public class GoogleAccountCredential implements HttpRequestInitializer

Beta
Manages authorization and account selection for Google accounts.

When fetching a token, any thrown GoogleAuthException would be wrapped:

Upgrade warning: in prior version 1.14 exponential back-off was enabled by default when I/O exception was thrown inside #getToken, but starting with version 1.15 you need to call #setBackOff with ExponentialBackOff to enable it.

Inheritance

java.lang.Object > GoogleAccountCredential

Implements

com.google.api.client.http.HttpRequestInitializer

Static Methods

usingAudience(Context context, String audience)

public static GoogleAccountCredential usingAudience(Context context, String audience)

Sets the audience scope to use with Google Cloud Endpoints.

Parameters
Name Description
context android.content.Context

context

audience String

audience

Returns
Type Description
GoogleAccountCredential

new instance

usingOAuth2(Context context, Collection<String> scopes)

public static GoogleAccountCredential usingOAuth2(Context context, Collection<String> scopes)

Constructs a new instance using OAuth 2.0 scopes.

Parameters
Name Description
context android.content.Context

context

scopes Collection<String>

non empty OAuth 2.0 scope list

Returns
Type Description
GoogleAccountCredential

new instance

Constructors

GoogleAccountCredential(Context context, String scope)

public GoogleAccountCredential(Context context, String scope)
Parameters
Name Description
context android.content.Context

context

scope String

scope to use on GoogleAuthUtil#getToken

Methods

getAllAccounts()

public final Account[] getAllAccounts()

Returns all Google accounts or null for none.

Returns
Type Description
android.accounts.Account[]

getBackOff()

public BackOff getBackOff()

Returns the back-off policy which is used when an I/O exception is thrown inside #getToken or null for none.

Returns
Type Description
com.google.api.client.util.BackOff

getContext()

public final Context getContext()

Returns the context.

Returns
Type Description
android.content.Context

getGoogleAccountManager()

public final GoogleAccountManager getGoogleAccountManager()

Returns the Google account manager.

Returns
Type Description
GoogleAccountManager

getScope()

public final String getScope()

Returns the scope to use on GoogleAuthUtil#getToken.

Returns
Type Description
String

getSelectedAccount()

public final Account getSelectedAccount()

Returns the selected Google account or null for none.

Returns
Type Description
android.accounts.Account

getSelectedAccountName()

public final String getSelectedAccountName()

Returns the selected Google account name (e-mail address), for example "johndoe@gmail.com", or null for none.

Returns
Type Description
String

getSleeper()

public final Sleeper getSleeper()

Returns the sleeper.

Returns
Type Description
com.google.api.client.util.Sleeper

getToken()

public String getToken()

Returns an OAuth 2.0 access token.

Must be run from a background thread, not the main UI thread.

Returns
Type Description
String
Exceptions
Type Description
IOException
com.google.android.gms.auth.GoogleAuthException

initialize(HttpRequest request)

public void initialize(HttpRequest request)
Parameter
Name Description
request com.google.api.client.http.HttpRequest

newChooseAccountIntent()

public final Intent newChooseAccountIntent()

Returns an intent to show the user to select a Google account, or create a new one if there are none on the device yet.

Must be run from the main UI thread.

Returns
Type Description
android.content.Intent

setBackOff(BackOff backOff)

public GoogleAccountCredential setBackOff(BackOff backOff)

Sets the back-off policy which is used when an I/O exception is thrown inside #getToken or null for none.

Parameter
Name Description
backOff com.google.api.client.util.BackOff
Returns
Type Description
GoogleAccountCredential

setSelectedAccount(Account selectedAccount)

public final GoogleAccountCredential setSelectedAccount(Account selectedAccount)

Sets the selected Google Account or null for none.

Caller must ensure the given Google account exists.

Parameter
Name Description
selectedAccount android.accounts.Account
Returns
Type Description
GoogleAccountCredential

setSelectedAccountName(String accountName)

public final GoogleAccountCredential setSelectedAccountName(String accountName)

Sets the selected Google account name (e-mail address) -- for example "johndoe@gmail.com" -- or null for none.

Parameter
Name Description
accountName String
Returns
Type Description
GoogleAccountCredential

setSleeper(Sleeper sleeper)

public final GoogleAccountCredential setSleeper(Sleeper sleeper)

Sets the sleeper. The default value is Sleeper#DEFAULT.

Parameter
Name Description
sleeper com.google.api.client.util.Sleeper
Returns
Type Description
GoogleAccountCredential