Updated FITBIT classes
This commit is contained in:
@@ -14,7 +14,6 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
testCompile group: 'junit', name: 'junit', version: '4.12'
|
testCompile group: 'junit', name: 'junit', version: '4.12'
|
||||||
// compile "com.sparkjava:spark-core:2.5.5"
|
// 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 "com.google.code.gson:gson:2.8.0"
|
||||||
// compile "org.xerial:sqlite-jdbc:3.15.1"
|
// compile "org.xerial:sqlite-jdbc:3.15.1"
|
||||||
compile 'org.apache.httpcomponents:httpclient:4.5.3'
|
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)
|
// 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 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.apache.commons:commons-lang3:3.4'
|
||||||
compile 'org.eclipse.jetty:jetty-client:9.3.11.v20160721'
|
compile 'org.eclipse.jetty:jetty-client:9.3.11.v20160721'
|
||||||
compile 'org.eclipse.jetty:jetty-http: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-client:9.3.12.v20160915'
|
||||||
compile 'org.eclipse.jetty.websocket:websocket-common:9.3.12.v20160915'
|
compile 'org.eclipse.jetty.websocket:websocket-common:9.3.12.v20160915'
|
||||||
compile 'org.slf4j:slf4j-simple:1.7.21'
|
compile 'org.slf4j:slf4j-simple:1.7.21'
|
||||||
|
|
||||||
|
//for objectMapper
|
||||||
|
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.5'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,28 +1,31 @@
|
|||||||
package manage;
|
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.AuthorizationCodeFlow;
|
||||||
import com.google.api.client.auth.oauth2.BearerToken;
|
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.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.java6.auth.oauth2.AuthorizationCodeInstalledApp;
|
||||||
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
|
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
|
||||||
import com.google.api.client.http.GenericUrl;
|
import com.google.api.client.http.*;
|
||||||
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.javanet.NetHttpTransport;
|
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.JsonFactory;
|
||||||
import com.google.api.client.json.JsonObjectParser;
|
import com.google.api.client.json.JsonObjectParser;
|
||||||
import com.google.api.client.json.jackson2.JacksonFactory;
|
import com.google.api.client.json.jackson2.JacksonFactory;
|
||||||
import com.google.api.client.util.store.DataStoreFactory;
|
import com.google.api.client.util.store.DataStoreFactory;
|
||||||
import com.google.api.client.util.store.FileDataStoreFactory;
|
import com.google.api.client.util.store.FileDataStoreFactory;
|
||||||
|
import manage.FITBITData.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
|
|
||||||
public class AuthFITBIT {
|
public class AuthFITBIT {
|
||||||
|
|
||||||
|
private static ObjectMapper mapper = new ObjectMapper();
|
||||||
/** Directory to store user credentials. */
|
/** 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
|
/* 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,
|
HTTP_TRANSPORT,
|
||||||
JSON_FACTORY,
|
JSON_FACTORY,
|
||||||
new GenericUrl(TOKEN_SERVER_URL),
|
new GenericUrl(TOKEN_SERVER_URL),
|
||||||
new ClientParametersAuthentication(
|
new BasicAuthentication(
|
||||||
OAuth2ClientCredentials.API_KEY, OAuth2ClientCredentials.API_SECRET),
|
OAuth2ClientCredentials.API_KEY, OAuth2ClientCredentials.API_SECRET),
|
||||||
OAuth2ClientCredentials.API_KEY,
|
OAuth2ClientCredentials.API_KEY,
|
||||||
AUTHORIZATION_SERVER_URL).setScopes(Arrays.asList(SCOPE))
|
AUTHORIZATION_SERVER_URL).setScopes(Arrays.asList(SCOPE))
|
||||||
@@ -72,31 +75,40 @@ public class AuthFITBIT {
|
|||||||
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost(
|
LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost(
|
||||||
OAuth2ClientCredentials.DOMAIN).setPort(OAuth2ClientCredentials.PORT).build();
|
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 {
|
private static void run(HttpRequestFactory requestFactory) throws IOException {
|
||||||
FITBITUrl url = new FITBITUrl("https://api.fitbit.com/1/user/-/profile.json"); //modificare con token?
|
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("activity,heartrate,location,sleep");
|
||||||
|
url.setFields("");
|
||||||
|
|
||||||
HttpRequest request = requestFactory.buildGetRequest(url);
|
HttpRequest request = requestFactory.buildGetRequest(url);
|
||||||
UserData data = request.execute().parseAs(UserData.class);
|
HttpResponse response = request.execute();
|
||||||
if (data.list.isEmpty()) {
|
|
||||||
System.out.println("Error in retrieve user data");
|
|
||||||
} else/* {
|
// System.out.println(response.parseAsString());
|
||||||
if (data.hasMore) {
|
|
||||||
System.out.print("First ");
|
|
||||||
}*/ //i don't think is necessary
|
try {
|
||||||
/* System.out.println(data.list.size() + " favorite videos found:");
|
GenericJson json = response.parseAs(GenericJson.class);
|
||||||
*/for (FITIBITData datas: data.list) {
|
|
||||||
System.out.println(datas.toString());/*
|
// Sleep sleep = mapper.readValue(json.toString(), Sleep.class);
|
||||||
System.out.println("-----------------------------------------------");
|
// Location location = mapper.readValue(json.toString(), Location.class);
|
||||||
System.out.println("ID: " + datas.id);
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
System.out.println("Title: " + datas.title);
|
HeartRate heart = mapper.readValue(json.toString(), HeartRate.class);
|
||||||
System.out.println("Tags: " + datas.tags);
|
System.out.println(json.toPrettyString());
|
||||||
System.out.println("URL: " + datas.url);
|
// Activity activity = mapper.readValue(json.toString(), Activity.class);
|
||||||
*/ }/*
|
|
||||||
}*/ //neither this
|
// System.out.println(json.toPrettyString());
|
||||||
|
response.disconnect();
|
||||||
|
}
|
||||||
|
catch(NullPointerException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
response.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -115,11 +127,18 @@ public class AuthFITBIT {
|
|||||||
System.err.print("DONE");
|
System.err.print("DONE");
|
||||||
// Success!
|
// Success!
|
||||||
return;
|
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);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
4
src/main/java/manage/FITBITData/Activity.java
Normal file
4
src/main/java/manage/FITBITData/Activity.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package manage.FITBITData;
|
||||||
|
|
||||||
|
public class Activity {
|
||||||
|
}
|
||||||
10
src/main/java/manage/FITBITData/HeartRate.java
Normal file
10
src/main/java/manage/FITBITData/HeartRate.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package manage.FITBITData;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
|
|
||||||
|
public class HeartRate {
|
||||||
|
|
||||||
|
@JsonProperty("dateTime")
|
||||||
|
public String date;
|
||||||
|
}
|
||||||
4
src/main/java/manage/FITBITData/Location.java
Normal file
4
src/main/java/manage/FITBITData/Location.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package manage.FITBITData;
|
||||||
|
|
||||||
|
public class Location {
|
||||||
|
}
|
||||||
4
src/main/java/manage/FITBITData/Sleep.java
Normal file
4
src/main/java/manage/FITBITData/Sleep.java
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package manage.FITBITData;
|
||||||
|
|
||||||
|
public class Sleep {
|
||||||
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user