"scroll to the BodyMedia connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");
// Record permanent failure since this connector won't work again until
// it is reauthenticated
guestService.setApiKeyStatus(updateInfo.apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
throw new UpdateFailedException("requires token reauthorization",true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
}
// We're not on a mirrored test server. Try to swap the expired
// access token for a fresh one.
// First, retrieve the OAuth server keys used when this key was created. These are automatically stored
// in the ApiKeyAttribute table at the time of creation based on the values present in
// oauth.properties.
String bodymediaConsumerKey = guestService.getApiKeyAttribute(updateInfo.apiKey, "bodymediaConsumerKey");
String bodymediaConsumerSecret = guestService.getApiKeyAttribute(updateInfo.apiKey, "bodymediaConsumerSecret");
OAuthConsumer consumer = new DefaultOAuthConsumer(
bodymediaConsumerKey,
bodymediaConsumerSecret);
String accessToken = guestService.getApiKeyAttribute(updateInfo.apiKey, "accessToken");
consumer.setTokenWithSecret(accessToken,
guestService.getApiKeyAttribute(updateInfo.apiKey, "tokenSecret"));
HttpParameters additionalParameter = new HttpParameters();
additionalParameter.put("api_key", bodymediaConsumerKey);
additionalParameter.put("oauth_token",
accessToken);
consumer.setAdditionalParameters(additionalParameter);
HttpClient httpClient = env.getHttpClient();
OAuthProvider provider = new CommonsHttpOAuthProvider(
"https://api.bodymedia.com/oauth/request_token?api_key="+bodymediaConsumerKey,
"https://api.bodymedia.com/oauth/access_token?api_key="+bodymediaConsumerKey,
"https://api.bodymedia.com/oauth/authorize?api_key="+bodymediaConsumerKey, httpClient);
try {
provider.retrieveAccessToken(consumer, null);
guestService.setApiKeyAttribute(updateInfo.apiKey,
"accessToken", consumer.getToken());
guestService.setApiKeyAttribute(updateInfo.apiKey,
"tokenSecret", consumer.getTokenSecret());
guestService.setApiKeyAttribute(updateInfo.apiKey,
"tokenExpiration", provider.getResponseParameters().get("xoauth_token_expiration_time").first());
// Record this connector as having status up
guestService.setApiKeyStatus(updateInfo.apiKey.getId(), ApiKey.Status.STATUS_UP, null, null);
// Schedule an update for this connector
connectorUpdateService.updateConnector(updateInfo.apiKey, false);
} catch (Throwable t) {
// Notify the user that the tokens need to be manually renewed
notificationsService.addNamedNotification(updateInfo.getGuestId(), Notification.Type.WARNING, connector().statusNotificationName(),
"Heads Up. We failed in our attempt to automatically refresh your authentication tokens.<br>" +
"Please head to <a href=\"javascript:App.manageConnectors()\">Manage Connectors</a>,<br>" +
"scroll to the BodyMedia connector, and renew your tokens (look for the <i class=\"icon-resize-small icon-large\"></i> icon)");
// Record permanent failure since this connector won't work again until
// it is reauthenticated
guestService.setApiKeyStatus(updateInfo.apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null, ApiKey.PermanentFailReason.NEEDS_REAUTH);
throw new UpdateFailedException("refresh token attempt failed", t, true, ApiKey.PermanentFailReason.NEEDS_REAUTH);
}
}