I’m building an Android app that needs to upload files to Google Drive using OAuth2 authentication. I can get the authorization token successfully, but when I try to exchange it for an access token, I get an “unauthorized_client” error.
Step 1 works fine - getting auth token:
String SCOPE = "oauth2:https://www.googleapis.com/auth/drive";
accManager.getAuthToken(
userAccount,
SCOPE,
null,
this,
new TokenCallback(),
new Handler(new ErrorHandler()));
class TokenCallback implements AccountManagerCallback<Bundle> {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle data = future.getResult();
String token = data.getString(AccountManager.KEY_AUTHTOKEN);
Log.d("Auth", "Got token: " + token);
new TokenExchange().execute();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Step 2 fails - exchanging for access token:
class TokenExchange extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... args) {
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory factory = new GsonFactory();
String APP_ID = "123456789.apps.googleusercontent.com";
String APP_SECRET = "mySecretKey123";
try {
GoogleTokenResponse response = new GoogleAuthorizationCodeTokenRequest(
httpTransport, factory, APP_ID, APP_SECRET, authToken, REDIRECT_URI
).execute();
String finalToken = response.getAccessToken();
Log.d("Final", "Access token: " + finalToken);
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
}
Error I’m getting:
com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error": "unauthorized_client"
}
I can access Google Drive normally on my device, so my account should be fine. What could be causing this unauthorized_client error? Am I using the wrong approach to exchange the token?