Obtaining OAuth2 Access Token for Google Drive API in Android App

I’m developing an application on Android that uploads data to Google Drive using OAuth2. The app is compatible with versions starting from ICS.

Process Overview:

In my first step, I successfully acquire an authorization token:

String SCOPE = "oauth2:https://www.googleapis.com/auth/drive";

mAccountManager.getAuthToken(
    selectedAccount,
    SCOPE,
    authOptions,
    this,
    new AuthTokenCallback(),
    new Handler(new TokenErrorHandler()));

private class AuthTokenCallback implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> future) {
        Bundle result;
        try {
            result = future.getResult();
            String authToken = result.getString(AccountManager.KEY_AUTHTOKEN);
            Log.d("Auth Token", "Received token: " + authToken);
            
            new AccessTokenExchange().execute();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Moving on to step two, I encounter issues while exchanging for the access token:

private class AccessTokenExchange extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... voids) {
        HttpTransport transport = new NetHttpTransport();
        JsonFactory jsonFactory = new GsonFactory();
        String CLIENT_ID = "999999999999.apps.googleusercontent.com";
        String CLIENT_SECRET = "yourClientSecretHere";
        
        try {
            GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
                transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, authToken, REDIRECT_URI
            ).execute();
            
            String accessToken = tokenResponse.getAccessToken();
            Log.d("Access Token", "Obtained token: " + accessToken);
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

I keep facing this error:

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error": "unauthorized_client"
}

I am able to use Google Drive without issues through the official app on my device, so my account seems valid. What could lead to this unauthorized_client error? Is my approach to exchanging tokens flawed?

The unauthorized_client error happens because your credentials aren’t set up right in Google Cloud Console. I hit this same issue - turns out my Android app wasn’t registered as an OAuth2 client properly. You need to create an Android client ID, not just a web one. Make sure the package name and SHA-1 fingerprint match your app exactly. Also check that Drive API is enabled for your project. Another thing that trips people up is using the wrong client credentials - use the Android client ID, not the web one. The previous answer’s right about the flow confusion, but even if you fix that, these credential mismatches will still cause unauthorized_client errors.

you’re overcomplicating this. the getAuthToken call already gives you a working access token for the Drive API. skip that second exchange step that’s breaking things - just use the token directly in your HTTP requests with the Bearer auth header. ditch the GoogleAuthorizationCodeTokenRequest completely.

You’re mixing up authentication flows. getAuthToken() already gives you a working OAuth2 access token, but you’re trying to run it through GoogleAuthorizationCodeTokenRequest - that’s for authorization codes, not tokens. I hit this same wall when I started with Google APIs on Android. The token from getAuthToken() IS your access token. Just stick it in the Authorization header as “Bearer [token]” and call the Drive API directly. Don’t do the exchange step. If you want to use Google’s client libraries, go with GoogleAccountCredential instead. Set it up with your account and scope, then feed it to your Drive service builder. Way cleaner and handles token refresh for you.