added method to retrieve heartbeats, needs to be tested
This commit is contained in:
@@ -1,9 +1,14 @@
|
||||
import manage.AuthFITBIT;
|
||||
import manage.FITBITData.HeartRate;
|
||||
import manage.FITBITData.Sleep;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
AuthFITBIT fitbit = new AuthFITBIT();
|
||||
|
||||
fitbit.run("https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json");
|
||||
HeartRate h = fitbit.run("https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json", HeartRate.class); // 1sec/time/00:00/00:01.json
|
||||
Sleep s = fitbit.run("https://api.fitbit.com/1.2/user/-/sleep/date/today.json", Sleep.class);
|
||||
|
||||
System.out.println(h.dateTime + " " + h.average);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
package manage;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
|
||||
import com.google.api.client.auth.oauth2.BearerToken;
|
||||
import com.google.api.client.auth.oauth2.Credential;
|
||||
import com.google.api.client.auth.oauth2.TokenResponseException;
|
||||
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
|
||||
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
|
||||
import com.google.api.client.http.*;
|
||||
@@ -18,7 +14,6 @@ import com.google.api.client.json.JsonObjectParser;
|
||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||
import com.google.api.client.util.store.DataStoreFactory;
|
||||
import com.google.api.client.util.store.FileDataStoreFactory;
|
||||
import manage.FITBITData.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
@@ -62,17 +57,11 @@ public class AuthFITBIT {
|
||||
/** Global instance of the JSON factory. */
|
||||
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
|
||||
|
||||
/* When i try to accept the request it'll send a 401 Unauthorized
|
||||
* on internet i found that this message appears when the client put a wrong
|
||||
* header, client_id or client_secret
|
||||
* https://dev.fitbit.com/build/reference/web-api/oauth2/ -> ALT+F "401 Unauthorized"
|
||||
*/
|
||||
private static final String TOKEN_SERVER_URL = " https://api.fitbit.com/oauth2/token";
|
||||
private static final String AUTHORIZATION_SERVER_URL = "https://www.fitbit.com/oauth2/authorize";
|
||||
|
||||
/** Authorizes the installed application to access user's protected data. */
|
||||
private Credential authorize() throws Exception {
|
||||
OAuth2ClientCredentials.errorIfNotSpecified();
|
||||
// set up authorization code flow
|
||||
AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken
|
||||
.authorizationHeaderAccessMethod(),
|
||||
@@ -91,23 +80,24 @@ public class AuthFITBIT {
|
||||
return new AuthorizationCodeInstalledApp(flow, receiver).authorize( "user" );
|
||||
}
|
||||
|
||||
public void run(String url) throws IOException {
|
||||
FITBITUrl fitbitUrl = new FITBITUrl(url); //modificare con token?
|
||||
public <O> O run(String url, Class<O> classe) throws IOException {
|
||||
FITBITUrl fitbitUrl = new FITBITUrl(url);
|
||||
// url.setFields("activity,heartrate,location,sleep");
|
||||
fitbitUrl.setFields("");
|
||||
|
||||
HttpRequest request = requestFactory.buildGetRequest(fitbitUrl);
|
||||
HttpResponse response = request.execute();
|
||||
|
||||
try {
|
||||
GenericJson json = response.parseAs(GenericJson.class);
|
||||
HeartRate heart = mapper.readValue(json.toString(), HeartRate.class);
|
||||
// System.out.println(json.toPrettyString());
|
||||
System.out.println(heart.dateTime);
|
||||
response.disconnect();
|
||||
} catch(NullPointerException e){
|
||||
e.printStackTrace();
|
||||
response.disconnect();
|
||||
}
|
||||
GenericJson json = response.parseAs(GenericJson.class);
|
||||
response.disconnect();
|
||||
|
||||
System.out.println("--------------------");
|
||||
System.out.println(classe.getSimpleName());
|
||||
System.out.println(json.toPrettyString());
|
||||
|
||||
return mapper.readValue(json.toString(), classe);
|
||||
|
||||
// System.out.println(json.toPrettyString());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,9 +9,20 @@ import java.util.Map;
|
||||
public class HeartRate {
|
||||
|
||||
public String dateTime;
|
||||
public double average;
|
||||
|
||||
@JsonProperty("activities-heart")
|
||||
public void quelloCheVoglio(Map<String, Object>[] activities){
|
||||
dateTime = (String) activities[0].get("dateTime");
|
||||
}
|
||||
|
||||
@JsonProperty("activities-heart-intraday")
|
||||
public void qualcosAltro(Map<String, Map<String, String>[]> map) {
|
||||
Map<String, String>[] data = map.get("dataset");
|
||||
|
||||
int sum = 0;
|
||||
for(Map<String, String> dat: data)
|
||||
sum += Integer.parseInt(dat.get("value"));
|
||||
average = ((double)sum)/data.length;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
package manage.FITBITData;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class Sleep {
|
||||
|
||||
public String time;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -13,14 +13,5 @@ public class OAuth2ClientCredentials {
|
||||
|
||||
/** Domain name in the "Callback URL". */
|
||||
public static final String DOMAIN = "127.0.0.1";
|
||||
|
||||
public static void errorIfNotSpecified() {
|
||||
if (API_KEY.startsWith("Enter ") || API_SECRET.startsWith("Enter ")) {
|
||||
System.out.println(
|
||||
"Enter API Key and API Secret from http://www.dailymotion.com/profile/developer"
|
||||
+ " into API_KEY and API_SECRET in " + OAuth2ClientCredentials.class);
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user