Class GoogleAccountCredential (1.35.0)

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
NameDescription
contextandroid.content.Context

context

audienceString

audience

Returns
TypeDescription
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
NameDescription
contextandroid.content.Context

context

scopesCollection<String>

non empty OAuth 2.0 scope list

Returns
TypeDescription
GoogleAccountCredential

new instance

Constructors

GoogleAccountCredential(Context context, String scope)

public GoogleAccountCredential(Context context, String scope)
Parameters
NameDescription
contextandroid.content.Context

context

scopeString

scope to use on GoogleAuthUtil#getToken

Methods

getAllAccounts()

public final Account[] getAllAccounts()

Returns all Google accounts or null for none.

Returns
TypeDescription
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
TypeDescription
com.google.api.client.util.BackOff

getContext()

public final Context getContext()

Returns the context.

Returns
TypeDescription
android.content.Context

getGoogleAccountManager()

public final GoogleAccountManager getGoogleAccountManager()

Returns the Google account manager.

Returns
TypeDescription
GoogleAccountManager

getScope()

public final String getScope()

Returns the scope to use on GoogleAuthUtil#getToken.

Returns
TypeDescription
String

getSelectedAccount()

public final Account getSelectedAccount()

Returns the selected Google account or null for none.

Returns
TypeDescription
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
TypeDescription
String

getSleeper()

public final Sleeper getSleeper()

Returns the sleeper.

Returns
TypeDescription
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
TypeDescription
String
Exceptions
TypeDescription
IOException
com.google.android.gms.auth.GoogleAuthException

initialize(HttpRequest request)

public void initialize(HttpRequest request)
Parameter
NameDescription
requestcom.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
TypeDescription
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
NameDescription
backOffcom.google.api.client.util.BackOff
Returns
TypeDescription
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
NameDescription
selectedAccountandroid.accounts.Account
Returns
TypeDescription
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
NameDescription
accountNameString
Returns
TypeDescription
GoogleAccountCredential

setSleeper(Sleeper sleeper)

public final GoogleAccountCredential setSleeper(Sleeper sleeper)

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

Parameter
NameDescription
sleepercom.google.api.client.util.Sleeper
Returns
TypeDescription
GoogleAccountCredential