Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android App: Acquire Access Token for Google Drive API

I am writing an Android (version ICS) app. which uploads data to the Google Drive. The app uses oauth2 to acquire the access token.

First step: acquire authorization token.

    String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive";
    // Step 1
    accountManager.getAuthToken(
        account,                                // Account retrieved using getAccountsByType("com.google")
        AUTH_TOKEN_TYPE,                        // Auth Token Type
        options,                                // Authenticator-specific options
        this,                                   // Your activity
        new OnTokenAcquired(),                  // Callback called when a token is successfully acquired
        new Handler(new OnAuthTokenError()));   // Callback called if an error occurs
}
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
    @Override
    public void run(AccountManagerFuture<Bundle> result) {
        // Get the result of the operation from the AccountManagerFuture.
        Bundle bundle;
        try {
            bundle = result.getResult();

            authToken = bundle.getString(AccountManager.KEY_AUTHTOKEN);

            Log.d(TAG,"authToken:" + authToken);

            exchangeToken access = (exchangeToken) new exchangeToken().execute();

        } catch (OperationCanceledException e) {
            e.printStackTrace();
        } catch (AuthenticatorException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
} 

Success. An authorization token is acquired.

Step 2: Exchange authorization token for Access Token.

    private class exchangeToken extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... params) {
        HttpTransport transport = new NetHttpTransport();
        JsonFactory jsonFactory = new GsonFactory();
        String CLIENT_ID = "999999999999.apps.googleusercontent.com";
        String CLIENT_SECRET = "axXXXXXXXXXXXXXXX7";

        try { // Step 2: Exchange for an access and refresh token
            GoogleTokenResponse authResponse = new GoogleAuthorizationCodeTokenRequest(transport, jsonFactory, CLIENT_ID, CLIENT_SECRET, authToken, CALLBACK_URL).execute();
            accessToken = authResponse.getAccessToken();
            Log.d("Get Access","Token:" + accessToken);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}  

Fail. The LogCat shows the following: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request

{

 "error":"unauthorized_client"

}

I have been able to access "Google Drive" on my Android tablet using the "Drive" app. so my email account is valid. May be the AUTH_TOKEN_TYPE is incorrect, but the Google Drive SDK is not clear what it must be. What am I missing?

like image 465
user1417943 Avatar asked Dec 04 '25 07:12

user1417943


1 Answers

You do not need to do the second step of exchanging the token. Android grants you an access token directly, it does not grant you an auth code which you would have to exchange for tokens.

This page on the Android documentation explains everything really well.

like image 150
Nicolas Garnier Avatar answered Dec 06 '25 01:12

Nicolas Garnier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!