oauthCallback += "?guestId=" + request.getParameter("guestId");
if (request.getParameter("apiKeyId") != null)
oauthCallback += "?apiKeyId=" + request.getParameter("apiKeyId");
String apiKey = env.get("bodymediaConsumerKey");
OAuthConsumer consumer = new DefaultOAuthConsumer(
apiKey,
env.get("bodymediaConsumerSecret"));
HttpParameters additionalParameter = new HttpParameters();
additionalParameter.put("api_key", apiKey);
consumer.setAdditionalParameters(additionalParameter);
HttpClient httpClient = env.getHttpClient();
OAuthProvider provider = new CommonsHttpOAuthProvider(
"https://api.bodymedia.com/oauth/request_token?api_key="+apiKey,
"https://api.bodymedia.com/oauth/access_token?api_key="+apiKey,
"https://api.bodymedia.com/oauth/authorize?api_key="+apiKey, httpClient);
request.getSession().setAttribute(BODYMEDIA_OAUTH_CONSUMER, consumer);
request.getSession().setAttribute(BODYMEDIA_OAUTH_PROVIDER, provider);
String approvalPageUrl = null;
try {
approvalPageUrl = provider.retrieveRequestToken(consumer,
oauthCallback);
} catch (Throwable t) {
logger.error("Couldn't retrieve BodyMedia request token.");
t.printStackTrace();
notificationsService.addNamedNotification(AuthHelper.getGuestId(),
Notification.Type.ERROR, connector().statusNotificationName(),
"Oops. There was an error with the BodyMedia API. " +
"Hang tight, we are working on it.");
// TODO: Should we record permanent failure since an existing connector won't work again until
// it is reauthenticated? We would need to get hold of the apiKey and do:
// guestService.setApiKeyStatus(apiKey.getId(), ApiKey.Status.STATUS_PERMANENT_FAILURE, null);
return "redirect:/app/";
}
System.out.println("the token secret is: " + consumer.getTokenSecret());
approvalPageUrl+="&oauth_api=" + apiKey;
approvalPageUrl = URLDecoder.decode(approvalPageUrl, "UTF-8");
return "redirect:" + approvalPageUrl;
}