Better gtasks error messages and human readable scope alias for auth token type

pull/14/head
Sam Bosley 13 years ago
parent c670f16bdd
commit 94422643aa

@ -49,9 +49,9 @@ public class GtasksListAdder extends Activity {
@Override
public void run() {
String token = gtasksPreferenceService.getToken();
token = GtasksTokenValidator.validateAuthToken(token);
GtasksService service = new GtasksService(token);
try {
token = GtasksTokenValidator.validateAuthToken(activity, token);
GtasksService service = new GtasksService(token);
String title = editText.getText().toString();
if (TextUtils.isEmpty(title)) //Don't create a list without a title
return;

@ -41,7 +41,7 @@ public class GtasksService {
private static final String API_KEY = "AIzaSyCIYZTBo6haRHxmiplZsfYdagFEpaiFnAk"; // non-production API key
public static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/tasks";
public static final String AUTH_TOKEN_TYPE = "Manage your tasks"; //"oauth2:https://www.googleapis.com/auth/tasks";
public GtasksService(String authToken) {
DependencyInjectionService.getInstance().inject(this);
@ -65,7 +65,7 @@ public class GtasksService {
int statusCode = h.getResponse().getStatusCode();
if (statusCode == 401 || statusCode == 403) {
System.err.println("Encountered " + statusCode + " error");
token = GtasksTokenValidator.validateAuthToken(token);
token = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token);
if (token != null) {
accessProtectedResource.setAccessToken(token);
}

@ -5,12 +5,15 @@ import java.io.IOException;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerFuture;
import android.content.Context;
import android.os.Bundle;
import com.google.api.client.googleapis.extensions.android2.auth.GoogleAccountManager;
import com.timsu.astrid.R;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.utility.Preferences;
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.api.GoogleTasksException;
import com.todoroo.astrid.gtasks.api.GtasksService;
public class GtasksTokenValidator {
@ -21,7 +24,7 @@ public class GtasksTokenValidator {
* @param token
* @return valid token on success, null on failure
*/
public static String validateAuthToken(String token) {
public static String validateAuthToken(Context c, String token) throws GoogleTasksException {
GoogleAccountManager accountManager = new GoogleAccountManager(ContextManager.getContext());
GtasksService testService = new GtasksService(token);
@ -30,10 +33,9 @@ public class GtasksTokenValidator {
return token;
} catch (IOException i) { //If fail, token may have expired -- get a new one and return that
String accountName = Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME);
Account a = accountManager.getAccountByName(Preferences.getStringValue(GtasksPreferenceService.PREF_USER_NAME));
Account a = accountManager.getAccountByName(accountName);
if (a == null) {
System.err.println("Account for name: " + accountName + " not found");
return null;
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountNotFound, accountName));
}
accountManager.invalidateAuthToken(token);
@ -49,20 +51,16 @@ public class GtasksTokenValidator {
return token;
} catch (IOException i2) {
i2.printStackTrace();
return null;
throw new GoogleTasksException(c.getString(R.string.gtasks_error_authRefresh));
}
} else {
System.err.println("Future did not have key for authtoken");
throw new GoogleTasksException(c.getString(R.string.gtasks_error_accountManager));
}
} catch (Exception e) {
e.printStackTrace();
return null;
throw new GoogleTasksException(e.getLocalizedMessage());
}
}
System.err.println("Gtasks token validation fell through all logic");
return null;
}
}

@ -9,6 +9,7 @@ import android.text.TextUtils;
import com.todoroo.andlib.data.DatabaseDao.ModelUpdateListener;
import com.todoroo.andlib.data.Property;
import com.todoroo.andlib.service.Autowired;
import com.todoroo.andlib.service.ContextManager;
import com.todoroo.andlib.service.DependencyInjectionService;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.andlib.utility.DateUtilities;
@ -21,7 +22,6 @@ import com.todoroo.astrid.gtasks.GtasksMetadata;
import com.todoroo.astrid.gtasks.GtasksMetadataService;
import com.todoroo.astrid.gtasks.GtasksPreferenceService;
import com.todoroo.astrid.gtasks.GtasksTaskListUpdater;
import com.todoroo.astrid.gtasks.api.GoogleTasksException;
import com.todoroo.astrid.gtasks.api.GtasksApiUtilities;
import com.todoroo.astrid.gtasks.api.GtasksService;
import com.todoroo.astrid.gtasks.api.MoveRequest;
@ -158,10 +158,7 @@ public final class GtasksSyncOnSaveService {
//Initialize the gtasks api service
String token = gtasksPreferenceService.getToken();
token = GtasksTokenValidator.validateAuthToken(token);
if (token == null) {
throw new GoogleTasksException("Failed to establish connection for sync on save"); //$NON-NLS-1$
}
token = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token);
gtasksPreferenceService.setToken(token);
GtasksService gtasksService = new GtasksService(token);
@ -265,10 +262,7 @@ public final class GtasksSyncOnSaveService {
AndroidUtilities.sleepDeep(1000L);
//Initialize the gtasks api service
String token = gtasksPreferenceService.getToken();
token = GtasksTokenValidator.validateAuthToken(token);
if (token == null) {
throw new GoogleTasksException("Failed to establish connection for sync on save"); //$NON-NLS-1$
}
token = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), token);
gtasksPreferenceService.setToken(token);
GtasksService gtasksService = new GtasksService(token);

@ -121,10 +121,7 @@ public class GtasksSyncProvider extends SyncProvider<GtasksTaskContainer> {
protected void initiateBackground() {
try {
String authToken = gtasksPreferenceService.getToken();
authToken = GtasksTokenValidator.validateAuthToken(authToken);
if (authToken == null) {
throw new GoogleTasksException(ContextManager.getContext().getString(R.string.gtasks_GLA_errorAuth));
}
authToken = GtasksTokenValidator.validateAuthToken(ContextManager.getContext(), authToken);
gtasksPreferenceService.setToken(authToken);
taskService = new GtasksService(authToken);

@ -96,6 +96,15 @@
<!-- Error Message when we receive a HTTP 503 error -->
<string name="gtasks_error_backend">Google\'s Task API is in beta and has encountered an error. The service may be down, please try again later.</string>
<!-- Error for account not found -->
<string name="gtasks_error_accountNotFound">Account %s not found--please log out and log back in from the Google Tasks settings.</string>
<!-- Error when ping after refreshing token fails -->
<string name="gtasks_error_authRefresh">Unable to authenticate with Google Tasks. Please check your account password or try again later.</string>
<!-- Error when account manager returns no auth token or throws exception -->
<string name="gtasks_error_accountManager">Error in your phone\'s account manager. Please log out and log back in from the Google Tasks settings.</string>

@ -38,7 +38,7 @@ public class GtasksApiTest extends DatabaseTestCase {
Task newTask = new Task();
String title = "New task";
newTask.setTitle(title);
service.createGtask(DEFAULT_LIST, newTask);
assertTrue(taskWithTitleExists(title));
}
@ -279,7 +279,7 @@ public class GtasksApiTest extends DatabaseTestCase {
}
String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
authToken = GtasksTokenValidator.validateAuthToken(authToken);
authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken);
service = new GtasksService(authToken);

@ -519,7 +519,7 @@ public class GtasksNewSyncTest extends DatabaseTestCase {
return;
}
String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
authToken = GtasksTokenValidator.validateAuthToken(authToken);
authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken);
gtasksPreferenceService.setToken(authToken);
gtasksService = new GtasksService(authToken);

@ -202,7 +202,7 @@ public class GtasksSyncOnSaveTest extends DatabaseTestCase {
return;
}
String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
authToken = GtasksTokenValidator.validateAuthToken(authToken);
authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken);
gtasksPreferenceService.setToken(authToken);
gtasksService = new GtasksService(authToken);

@ -136,7 +136,7 @@ public class RepeatTestsGtasksSync extends NewRepeatTests<com.google.api.service
return;
}
String authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
authToken = GtasksTokenValidator.validateAuthToken(authToken);
authToken = GtasksTokenValidator.validateAuthToken(getContext(), authToken);
gtasksPreferenceService.setToken(authToken);
gtasksService = new GtasksService(authToken);

Loading…
Cancel
Save