1 package com.google.appinventor.components.runtime.util;
3 import java.io.IOException;
5 import com.
google.api.client.googleapis.auth.oauth2.GoogleCredential;
6 import com.
google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
8 import android.accounts.Account;
9 import android.accounts.AccountManager;
10 import android.accounts.AccountManagerFuture;
11 import android.accounts.AuthenticatorException;
12 import android.accounts.OperationCanceledException;
13 import android.app.Activity;
14 import android.content.SharedPreferences;
16 import android.os.Bundle;
17 import android.os.Looper;
18 import android.util.Log;
73 public static final String
TAG =
"OAuthHelper";
77 private static String errorMessage =
"Error during OAuth";
89 Log.i(
TAG,
"getRefreshedAuthToken()");
92 throw new IllegalArgumentException(
"Can't get authtoken from UI thread");
95 SharedPreferences settings = activity.getPreferences(Activity.MODE_PRIVATE);
102 GoogleCredential credential =
new GoogleCredential();
103 credential.setAccessToken(authToken);
107 AccountManagerFuture<Bundle> future =
108 getAccountManagerResult(activity, credential, authTokenType, accountName);
113 Bundle authTokenBundle = future.getResult();
114 authToken = authTokenBundle.get(AccountManager.KEY_AUTHTOKEN).toString();
116 persistCredentials(settings,
117 authTokenBundle.getString(AccountManager.KEY_ACCOUNT_NAME),
119 }
catch (OperationCanceledException e) {
122 errorMessage =
"Error: operation cancelled";
123 }
catch (AuthenticatorException e) {
125 errorMessage =
"Error: Authenticator error";
126 }
catch (IOException e) {
128 errorMessage =
"Error: I/O error";
145 private AccountManagerFuture<Bundle> getAccountManagerResult(Activity activity,
146 GoogleCredential credential,
147 String authTokenType,
148 String accountName) {
150 AccountManagerFuture<Bundle> future =
null;
151 GoogleAccountManager accountManager =
new GoogleAccountManager(activity);
158 accountManager.invalidateAuthToken(credential.getAccessToken());
159 AccountManager.get(activity).invalidateAuthToken(authTokenType,
null);
163 Account account = accountManager.getAccountByName(accountName);
166 if (account !=
null) {
169 Log.i(
TAG,
"Getting token by account");
170 future = accountManager.getAccountManager().getAuthToken(account, authTokenType,
true,
null,
null);
175 Log.i(
TAG,
"Getting token by features, possibly prompting user to select an account");
176 future = accountManager.getAccountManager().getAuthTokenByFeatures(GoogleAccountManager.ACCOUNT_TYPE,
177 authTokenType,
null, activity,
null,
null,
null,
null);
187 private boolean isUiThread() {
188 return Looper.getMainLooper().getThread().equals(Thread.currentThread());
197 private void persistCredentials(SharedPreferences settings, String accountName, String authToken) {
198 Log.i(
TAG,
"Persisting credentials, acct =" + accountName);
199 SharedPreferences.Editor editor = settings.edit();
212 Log.i(
TAG,
"Reset credentials");
213 SharedPreferences settings = activity.getPreferences(Activity.MODE_PRIVATE);
214 SharedPreferences.Editor editor2 = settings.edit();
225 Log.i(
TAG,
"getErrorMessage = " + errorMessage);