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?