Updated FITBIT classes

This commit is contained in:
Giulia
2018-06-06 15:40:29 +02:00
parent 4695b9ddfc
commit 2ff959d3e7
8 changed files with 76 additions and 72 deletions

View File

@@ -14,7 +14,6 @@ repositories {
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
// compile "com.sparkjava:spark-core:2.5.5"
// compile "org.slf4j:slf4j-simple:1.7.21"
compile "com.google.code.gson:gson:2.8.0"
// compile "org.xerial:sqlite-jdbc:3.15.1"
compile 'org.apache.httpcomponents:httpclient:4.5.3'
@@ -23,9 +22,8 @@ dependencies {
// z-way-lib and all its dependencies (from https://github.com/pathec/ZWay-library-for-Java)
compile files('lib/zway-lib-0.2.9-SNAPSHOT.jar')
// compile 'com.google.code.gson:gson:2.4' //already up there
compile 'org.apache.commons:commons-lang3:3.4'
compile 'org.eclipse.jetty:jetty-client:9.3.11.v20160721'
compile 'org.eclipse.jetty:jetty-http:9.3.11.v20160721'
@@ -35,4 +33,7 @@ dependencies {
compile 'org.eclipse.jetty.websocket:websocket-client:9.3.12.v20160915'
compile 'org.eclipse.jetty.websocket:websocket-common:9.3.12.v20160915'
compile 'org.slf4j:slf4j-simple:1.7.21'
//for objectMapper
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'
}

View File

@@ -1,28 +1,31 @@
package manage;
import com.fasterxml.jackson.databind.DeserializationFeature;
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.ClientParametersAuthentication;
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.GenericUrl;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestFactory;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.*;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.GenericJson;
import com.google.api.client.json.JsonFactory;
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;
public class AuthFITBIT {
private static ObjectMapper mapper = new ObjectMapper();
/** Directory to store user credentials. */
/* Throw a Warning when change permission: they said it's a google bug 'cause is meant to run in linux/unix
*
@@ -63,7 +66,7 @@ public class AuthFITBIT {
HTTP_TRANSPORT,
JSON_FACTORY,
new GenericUrl(TOKEN_SERVER_URL),
new ClientParametersAuthentication(
new BasicAuthentication(
OAuth2ClientCredentials.API_KEY, OAuth2ClientCredentials.API_SECRET),
OAuth2ClientCredentials.API_KEY,
AUTHORIZATION_SERVER_URL).setScopes(Arrays.asList(SCOPE))
@@ -72,31 +75,40 @@ public class AuthFITBIT {
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost(
OAuth2ClientCredentials.DOMAIN).setPort(OAuth2ClientCredentials.PORT).build();
return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user" );
return new AuthorizationCodeInstalledApp(flow, receiver).authorize( "user" );
}
private static void run(HttpRequestFactory requestFactory) throws IOException {
FITBITUrl url = new FITBITUrl("https://api.fitbit.com/1/user/-/profile.json"); //modificare con token?
url.setFields("activity,heartrate,location,sleep");
FITBITUrl url = new FITBITUrl("https://api.fitbit.com/1/user/-/activities/heart/date/today/1d.json"); //modificare con token?
// url.setFields("activity,heartrate,location,sleep");
url.setFields("");
HttpRequest request = requestFactory.buildGetRequest(url);
UserData data = request.execute().parseAs(UserData.class);
if (data.list.isEmpty()) {
System.out.println("Error in retrieve user data");
} else/* {
if (data.hasMore) {
System.out.print("First ");
}*/ //i don't think is necessary
/* System.out.println(data.list.size() + " favorite videos found:");
*/for (FITIBITData datas: data.list) {
System.out.println(datas.toString());/*
System.out.println("-----------------------------------------------");
System.out.println("ID: " + datas.id);
System.out.println("Title: " + datas.title);
System.out.println("Tags: " + datas.tags);
System.out.println("URL: " + datas.url);
*/ }/*
}*/ //neither this
HttpResponse response = request.execute();
// System.out.println(response.parseAsString());
try {
GenericJson json = response.parseAs(GenericJson.class);
// Sleep sleep = mapper.readValue(json.toString(), Sleep.class);
// Location location = mapper.readValue(json.toString(), Location.class);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
HeartRate heart = mapper.readValue(json.toString(), HeartRate.class);
System.out.println(json.toPrettyString());
// Activity activity = mapper.readValue(json.toString(), Activity.class);
// System.out.println(json.toPrettyString());
response.disconnect();
}
catch(NullPointerException e){
e.printStackTrace();
response.disconnect();
}
}
public static void main(String[] args) {
@@ -115,11 +127,18 @@ public class AuthFITBIT {
System.err.print("DONE");
// Success!
return;
} catch (IOException e) {
System.err.println(e.getClass().getSimpleName()+" "+e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
catch (TokenResponseException e) {
e.printStackTrace();
}
catch (IOException e) {
System.err.println(e.getClass().getSimpleName() + " " + e.getMessage());
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
System.exit(1);
}

View File

@@ -0,0 +1,4 @@
package manage.FITBITData;
public class Activity {
}

View File

@@ -0,0 +1,10 @@
package manage.FITBITData;
import com.fasterxml.jackson.annotation.JsonProperty;
public class HeartRate {
@JsonProperty("dateTime")
public String date;
}

View File

@@ -0,0 +1,4 @@
package manage.FITBITData;
public class Location {
}

View File

@@ -0,0 +1,4 @@
package manage.FITBITData;
public class Sleep {
}

View File

@@ -1,21 +0,0 @@
package manage;
import com.google.api.client.util.Key;
import java.util.List;
public class FITIBITData {
@Key
public String activity;
@Key
public String heartrate;
@Key
public String sleep;
@Key
public String location;
}

View File

@@ -1,17 +0,0 @@
package manage;
import com.google.api.client.util.Key;
import java.util.List;
public class UserData {
@Key
public List<FITIBITData> list;
/*
@Key
public int limit;
@Key("has more")
public boolean hasMore;*/ //don't think are needed
}