responseBody = StreamUtil.readString(reader);
} finally {
StreamUtil.close(reader);
}
} catch (Exception e) {
throw new OAuthException(e ,"refreshAccessToken failed with Exception: <br>" + e);
} finally {
if (method != null){
try {
if(content!=null) {
content.close();
}
} catch (IOException e) {
throw new OAuthException(e ,"refreshAccessToken failed with Exception: <br>" + e);
}
}
}
if (responseCode != HttpStatus.SC_OK) {
acquireToken(true, true); // Failed to renew token, get a new one
return null;
} else {
setOAuthData(responseBody);
renewedtoken = createToken(getAppId(),getServiceName()); // Now create a new token and save that in the store
Context context = Context.get();
setAccessTokenObject(renewedtoken);
try {
if(!context.isCurrentUserAnonymous()) {
CredentialStore credStore = findCredentialStore();
if (credStore != null) {
// if the token is already present, and was expired due to which we have fetched a new
// token, then we remove the token from the store first and then add this new token.
deleteToken();
credStore.store(getServiceName(), ACCESS_TOKEN_STORE_TYPE, getUserId(), token);
}
} else {
AnonymousCredentialStore.storeCredentials(context, token, getAppId(), getServiceName()); // Store the token for anonymous user
}
} catch (CredentialStoreException cse) {
throw new OAuthException(cse, "Error trying to renew Token.");
}
}
return renewedtoken;
}