Google Plus Access Token Cache issue
- Dominant language
- Java
- Stars
- 877
- Forks
- 225
- PR merge metrics
- No merged PRs in 30d
Description
If a user receives an access token but does not complete the login until it expires, the user will not be able to log in. This could also be seen if the user revokes the access token. This is due to the GoogleAuthUtil caching the access token on the device for an unknown period of time.
In the code written below, the access token is checked for validity, and if not valid, it will clear the cached token and request a new one.
I can send in a pull request if you would like.
```
@Override
public void requestAccessToken(OnRequestAccessTokenCompleteListener onRequestAccessTokenCompleteListener) {
super.requestAccessToken(onRequestAccessTokenCompleteListener);
AsyncTask task = new AsyncTask() {
@Override
protected String doInBackground(Activity... params) {
String scope = "oauth2:" + Scopes.PLUS_LOGIN;
String token;
String error = null;
try {
token = GoogleAuthUtil.getToken(params[0],
Plus.AccountApi.getAccountName(googleApiClient), scope);
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
HttpClient client = new DefaultHttpClient(httpParameters);
String url = "https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + token;
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null; ) {
builder.append(line).append("\n");
}
JSONTokener tokener = new JSONTokener(builder.toString());
JSONObject finalResult = new JSONObject(tokener);
error = finalResult.getString("error");
} catch (Exception e) {
//Probably shouldn't use Exception E here but there are quite a few
//http/io/json exceptions that could occur
}
if (error != null && error.equals("invalid_token")) {
GoogleAuthUtil.clearToken(params[0], token);
token = GoogleAuthUtil.getToken(params[0],
Plus.AccountApi.getAccountName(googleApiClient), scope);
}
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
return token;
}
@Override
protected void onPostExecute(String token) {
if(token != null) {
((OnRequestAccessTokenCompleteListener) mLocalListeners.get(REQUEST_ACCESS_TOKEN))
.onRequestAccessTokenComplete(getID(), new AccessToken(token, null));
} else {
mLocalListeners.get(REQUEST_ACCESS_TOKEN).onError(getID(), REQUEST_ACCESS_TOKEN, token, null);
}
}
};
task.execute(mActivity);
}
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.