AI2 Component  (Version nb184)
com.google.appinventor.components.runtime.util.OAuth2Helper Class Reference

Public Member Functions

 OAuth2Helper ()
 
String getRefreshedAuthToken (Activity activity, String authTokenType)
 

Static Public Member Functions

static void resetAccountCredential (Activity activity)
 
static String getErrorMessage ()
 

Static Public Attributes

static final String TAG = "OAuthHelper"
 
static final String PREF_AUTH_TOKEN = "authToken"
 
static final String PREF_ACCOUNT_NAME = "accountName"
 

Detailed Description

This helper class uses AccountManager to handle OAuth 2.0 authorization, returning an access_token that is needed for Fusiontables requests. The helper contains no reference to Fusiontables, so perhaps it can be generalized to handle OAuth for other Google APIs.

Author
rmorelli

This follows examples in the Google API Client libraries, especially the tasks-android-sample and the fusiontables-commandline-sample.

Here's an good overview: https://developers.google.com/accounts/docs/OAuth2

Here's how it works.

Access by an app (a client) to an shared resource (a fusiontable) requires the app to obtain an access token. The access token must be attached in the Http request header for every query to the shared resource. The access token is usually valid for 1 hour and then it must be refreshed.

In order to obtain access to the resource the app must use an API key. This is obtained by registering through your Google API console.

Google AccountManager will manage the request for the access token, hiding most of the details. There are two cases:

1) The app has stored the user's account name and the user is already logged in to a Google account. In this case AccountManager uses the account to obtain an access token.

2) The app has not yet stored the user's account name. In this case the AccountManager uses "features" to obtain the token, prompting the user to choose an account if there are more than one on the phone.

In either case OAuthHelper saves the user's account name and acquired access token (called, traditionally, authToken) in shared preferences.

Access/Refresh Token

If an expired access token is passed to a fusiontables query, the query will be rejected with a 401 error. In that case, the app must get a fresh token. This can be done by using a 'refresh token'.
But it can also be done more simply by discarding the saved access token and acquiring a fresh one for each query. That's the approach taken here.

Comments in the code provide the details.

Definition at line 71 of file OAuth2Helper.java.

Constructor & Destructor Documentation

◆ OAuth2Helper()

com.google.appinventor.components.runtime.util.OAuth2Helper.OAuth2Helper ( )

Definition at line 79 of file OAuth2Helper.java.

Member Function Documentation

◆ getErrorMessage()

static String com.google.appinventor.components.runtime.util.OAuth2Helper.getErrorMessage ( )
static

Clients can retrieve error messages statically.

Returns
errorMessage

Definition at line 224 of file OAuth2Helper.java.

◆ getRefreshedAuthToken()

String com.google.appinventor.components.runtime.util.OAuth2Helper.getRefreshedAuthToken ( Activity  activity,
String  authTokenType 
)

Uses account manager to return a fresh authToken of authTokenType

Parameters
activity
authTokenTypea service such as "fusiontables"
Returns
an access token string that must be added to the OAuth header in all resource queries, or null if something goes wrong.

Definition at line 88 of file OAuth2Helper.java.

◆ resetAccountCredential()

static void com.google.appinventor.components.runtime.util.OAuth2Helper.resetAccountCredential ( Activity  activity)
static

Forget the account name and authToken. With no account name the app will prompt the user to select a new account. This method is mostly used for testing purposes.

Parameters
activity

Definition at line 211 of file OAuth2Helper.java.

Member Data Documentation

◆ PREF_ACCOUNT_NAME

final String com.google.appinventor.components.runtime.util.OAuth2Helper.PREF_ACCOUNT_NAME = "accountName"
static

Definition at line 75 of file OAuth2Helper.java.

◆ PREF_AUTH_TOKEN

final String com.google.appinventor.components.runtime.util.OAuth2Helper.PREF_AUTH_TOKEN = "authToken"
static

Definition at line 74 of file OAuth2Helper.java.

◆ TAG

final String com.google.appinventor.components.runtime.util.OAuth2Helper.TAG = "OAuthHelper"
static

Definition at line 73 of file OAuth2Helper.java.


The documentation for this class was generated from the following file: