Skip to content

Instantly share code, notes, and snippets.

@MoriTanosuke
Last active December 30, 2015 13:59
Show Gist options
  • Save MoriTanosuke/7839061 to your computer and use it in GitHub Desktop.
Save MoriTanosuke/7839061 to your computer and use it in GitHub Desktop.
Example code for my blog post on Fitbit webapp example
get(new Route("/login") {
@Override
public Object handle(Request request, Response response) {
if (request.queryParams("completeAuthorization") != null) {
// Get temporary token and verifier returned by Fitbit from query string
String tempTokenReceived = request.queryParams("oauth_token");
String tempTokenVerifier = request.queryParams("oauth_verifier");
// Fetch user credentials from cache by temporary token from query string
APIResourceCredentials resourceCredentials = fitbit.getResourceCredentialsByTempToken(tempTokenReceived);
// Call method of Fitbit4J to get token credentials only if necessary (they haven't been cached yet)
if (!resourceCredentials.isAuthorized()) {
// The verifier is required in the request to get token credentials
resourceCredentials.setTempTokenVerifier(tempTokenVerifier);
// Get token credentials for the user
LocalUserDetail localUser = new LocalUserDetail(resourceCredentials.getLocalUserId());
fitbit.getTokenCredentials(localUser);
// set user into session
request.session().attribute("user", localUser);
request.session().attribute("client", fitbit.getClient());
}
// redirect to step chart
response.redirect("/steps");
} else {
// Fetch temporary credentials, construct redirect and serve it to user's browser
response.redirect(fitbit.getResourceOwnerAuthorizationURL(new LocalUserDetail("-"),
BASE_URL + "/login?completeAuthorization="));
}
}
});
private static List<Data> loadData(final LocalUserDetail localUser, FitbitApiClientAgent client, LocalDate startDate, FitbitUser fitbitUser, TimeSeriesResourceType type, TimePeriod period) throws FitbitAPIException {
return client.getTimeSeries(localUser, fitbitUser, type, startDate, period);
}
<html>
<head>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Steps');
data.addRows([
<#list data as d>
[new Date('${d.dateTime}'), ${d.value}],
</#list>
]);
var options = {'title':'Steps', 'width':document.width - 50, 'height':document.height - 150};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
<html>
get(new FreeMarkerRoute("/steps") {
@Override
public ModelAndView handle(Request request, Response response) {
final LocalUserDetail localUser = request.session().attribute("user");
final FitbitApiClientAgent client = request.session().attribute("client");
Map<String, Object> attributes = new HashMap<String, Object>();
String view = "steps.ftl";
LocalDate startDate = FitbitApiService.getValidLocalDateOrNull(today());
List<Data> data = loadData(localUser, client, startDate, new FitbitUser("-"), TimeSeriesResourceType.STEPS, TimePeriod.ONE_WEEK);
attributes.put("data", data);
return modelAndView(attributes, view);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment