return "redirect:" + approvalPageUrl;
}
@RequestMapping(value = "/upgradeToken")
public String upgradeToken(HttpServletRequest request) throws Exception {
OAuthConsumer consumer = (OAuthConsumer) request.getSession()
.getAttribute(WITHINGS_OAUTH_CONSUMER);
HttpParameters additionalParameter = new HttpParameters();
String userid = request.getParameter("userid");
additionalParameter.put("userid", userid);
consumer.setAdditionalParameters(additionalParameter);
OAuthProvider provider = (OAuthProvider) request.getSession()
.getAttribute(WITHINGS_OAUTH_PROVIDER);
String verifier = request.getParameter("oauth_verifier");
provider.retrieveAccessToken(consumer, verifier);
Guest guest = AuthHelper.getGuest();
Connector connector = Connector.getConnector("withings");
ApiKey apiKey;
if (request.getSession().getAttribute(WITHINGS_RENEWTOKEN_APIKEYID)!=null) {
final String apiKeyIdString = (String) request.getSession().getAttribute(WITHINGS_RENEWTOKEN_APIKEYID);
long apiKeyId = Long.valueOf(apiKeyIdString);
apiKey = guestService.getApiKey(apiKeyId);
} else {
apiKey = guestService.createApiKey(guest.getId(), connector);
}
// We need to store the consumer ID and secret with the
// apiKeyAttributes in either the case of original creation of the key
// or token renewal. createApiKey actually handles the former case, but
// not the latter. Do it in all cases here.
guestService.setApiKeyAttribute(apiKey, "withingsConsumerKey",
env.get("withingsConsumerKey"));
guestService.setApiKeyAttribute(apiKey, "withingsConsumerSecret",
env.get("withingsConsumerSecret"));
guestService.setApiKeyAttribute(apiKey,
"accessToken", consumer.getToken());
guestService.setApiKeyAttribute(apiKey,
"tokenSecret", consumer.getTokenSecret());
guestService.setApiKeyAttribute(apiKey,
"userid", userid);
guestService.setApiKeyAttribute(apiKey,
HAS_UPGRADED_TO_OAUTH, "y");