throws OAuthException {
Credential credential = null;
LOGGER.debug("Retrieving Auth Token from DB.");
AuthMcc authMcc = this.getAuthTokenFromStorage(mccAccountId);
String authToken = null;
// Generate a new Auth Token if necessary
if (authMcc == null || authMcc.getScope() == null
|| !authMcc.getScope().equals(scope) || force) {
try {
LOGGER.debug("Auth Token FORCED. Getting a new one.");
credential = getNewOAuth2Credential();
} catch (OAuthException e) {
if (e.getMessage().contains("Connection reset")) {
LOGGER.info("Connection reset when getting Auth Token, retrying...");
credential = getNewOAuth2Credential();
} else {
LOGGER.error("Error Authenticating: " + e.getMessage());
e.printStackTrace();
throw e;
}
} finally {
if (credential != null) {
// Try to get the MCC Company Name and DescriptiveName
String name = "";
try {
AdWordsSession adWordsSession = authenticate(null, mccAccountId, credential).build();
CustomerDelegate customerDelegate = new CustomerDelegate(adWordsSession);
Customer customer = customerDelegate.getCustomer();
name = customer.getCompanyName() + " (" + customer.getDescriptiveName() + ")";
} catch (ValidationException e) {
LOGGER.error("Error trying to get MCC Name " + e.getMessage());
} catch (ApiException e) {
LOGGER.error("Error trying to get MCC Name " + e.getMessage());
}
LOGGER.info("Saving Refresh Token to DB...");
this.saveAuthTokenToStorage(mccAccountId, name, credential.getRefreshToken(), scope);
}
}
} else {
authToken = authMcc.getAuthToken();
credential = buildOAuth2Credentials(authToken);
}
return credential;
}