Android: Connecting to Google Docs Using AccountManager

Hey folks, I’m stuck on a project where I need to hook up my Android app to Google Docs. I started with the Tasks API example, but I’m hitting a wall with a ‘401 Cannot parse AuthSub token’ error.

I’ve been digging through the docs and forums, and it looks like Android might be spitting out the wrong kind of access token. Anyone know if there’s a trick to getting a valid token with AccountManager? Or should I be looking at a completely different method for authentication?

Here’s a snippet of what I’ve got so far:

public class DocsFetcher extends Activity {
    private static final String AUTH_TYPE = 'docs_access';
    private GoogleApiClient mGoogleApiClient;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupGoogleApiClient();
    }
    
    private void setupGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Auth.GOOGLE_SIGN_IN_API)
            .build();
        
        // More setup code here...
    }
    
    private void fetchDocs() {
        // Code to fetch docs using the client
        // This is where I'm getting stuck
    }
}

Any ideas on how to get this working? I’m pretty new to the Google APIs, so I might be missing something obvious. Thanks in advance!

I’ve actually faced a similar issue when working with Google Docs API in Android. The ‘401 Cannot parse AuthSub token’ error is often related to outdated authentication methods. Here’s what worked for me:

Instead of using AccountManager directly, I switched to using the Google Sign-In API for authentication. It’s more straightforward and handles a lot of the token management for you.

First, add the Google Sign-In dependency to your build.gradle:

implementation ‘com.google.android.gms:play-services-auth:20.5.0’

Then, set up the GoogleSignInClient in your activity:

GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestScopes(new Scope(DocsScopes.DRIVE_FILE))
.build();
GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

Implement the sign-in process and handle the result. Once signed in, you can use the account to initialize your Drive service:

GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(
this, Collections.singleton(DocsScopes.DRIVE_FILE));
credential.setSelectedAccount(account.getAccount());

Drive service = new Drive.Builder(
AndroidHttp.newCompatibleTransport(),
new GsonFactory(),
credential)
.setApplicationName(“Your App Name”)
.build();

This approach has been more reliable for me when connecting to Google Docs. Hope it helps!

hey ethan, i ran into this too. try using the google sign-in api instead of accountmanager. it’s way easier. add the dependency, setup GoogleSignInClient, and handle the sign-in process. then use the account to init your Drive service. should fix that 401 error. good luck!

When dealing with authentication problems on Android, I found success by using the Google Sign-In API combined with the Drive API for accessing Google Docs. First, ensure your project in the Google Developer Console has all the necessary APIs enabled. In your code, use GoogleSignInClient for authentication to obtain a valid GoogleSignInAccount. You can then convert this account into a GoogleAccountCredential and build your Drive service with it. This approach has worked reliably in my experience and should help you avoid the 401 error.