From 7d28391db0c1dbbd6fa9d6574bcdd416e85cae4f Mon Sep 17 00:00:00 2001 From: Giulia <20015091@studenti.uniupo.it> Date: Tue, 29 May 2018 18:18:13 +0200 Subject: [PATCH] Added classes for manage authentication and retrieve data to/from fitbit... but the code needs some mods! --- build.gradle | 3 + src/main/java/manage/AuthFITBIT.java | 116 ++++++++++++++++++ src/main/java/manage/Data.java | 22 ++++ src/main/java/manage/FITBITUrl.java | 28 +++++ .../java/manage/OAuth2ClientCredentials.java | 26 ++++ src/main/java/manage/UserData.java | 17 +++ 6 files changed, 212 insertions(+) create mode 100644 src/main/java/manage/AuthFITBIT.java create mode 100644 src/main/java/manage/Data.java create mode 100644 src/main/java/manage/FITBITUrl.java create mode 100644 src/main/java/manage/OAuth2ClientCredentials.java create mode 100644 src/main/java/manage/UserData.java diff --git a/build.gradle b/build.gradle index f00b77a..b6f218b 100644 --- a/build.gradle +++ b/build.gradle @@ -18,5 +18,8 @@ dependencies { 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' + compile 'com.google.api-client:google-api-client:1.23.0' + compile group: 'com.google.oauth-client', name: 'google-oauth-client-jetty', version: '1.11.0-beta' + } diff --git a/src/main/java/manage/AuthFITBIT.java b/src/main/java/manage/AuthFITBIT.java new file mode 100644 index 0000000..b2497a3 --- /dev/null +++ b/src/main/java/manage/AuthFITBIT.java @@ -0,0 +1,116 @@ +package manage; + +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.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.javanet.NetHttpTransport; +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 java.io.IOException; +import java.util.Arrays; + +public class AuthFITBIT { + + /** Directory to store user credentials. */ + private static final java.io.File DATA_STORE_DIR = + new java.io.File(System.getProperty("user.home"), ".store/dailymotion_sample"); + + /** + * Global instance of the {@link DataStoreFactory}. The best practice is to make it a single + * globally shared instance across your application. + */ + private static FileDataStoreFactory DATA_STORE_FACTORY; + + /** OAuth 2 scope. */ + private static final String SCOPE = "read"; + + /** Global instance of the HTTP transport. */ + private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); + + /** Global instance of the JSON factory. */ + static final JsonFactory JSON_FACTORY = new JacksonFactory(); + + private static final String TOKEN_SERVER_URL = " https://api.fitbit.com/oauth2/token"; + private static final String AUTHORIZATION_SERVER_URL = + "https://api.fitbit.com/oauth2/authorize"; + + /** Authorizes the installed application to access user's protected data. */ + private static Credential authorize() throws Exception { + OAuth2ClientCredentials.errorIfNotSpecified(); + // set up authorization code flow + AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken + .authorizationHeaderAccessMethod(), + HTTP_TRANSPORT, + JSON_FACTORY, + new GenericUrl(TOKEN_SERVER_URL), + new ClientParametersAuthentication( + OAuth2ClientCredentials.API_KEY, OAuth2ClientCredentials.API_SECRET), + OAuth2ClientCredentials.API_KEY, + AUTHORIZATION_SERVER_URL).setScopes(Arrays.asList(SCOPE)) + .setDataStoreFactory(DATA_STORE_FACTORY).build(); + // authorize + LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost( + OAuth2ClientCredentials.DOMAIN).setPort(OAuth2ClientCredentials.PORT).build(); + 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"); + + 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 (Data datas: data.list) { + System.out.println(); + 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 + } + + public static void main(String[] args) { + try { + DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); + final Credential credential = authorize(); + HttpRequestFactory requestFactory = + HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { + @Override + public void initialize(HttpRequest request) throws IOException { + credential.initialize(request); + request.setParser(new JsonObjectParser(JSON_FACTORY)); + } + }); + run(requestFactory); + // Success! + return; + } catch (IOException e) { + System.err.println(e.getMessage()); + } catch (Throwable t) { + t.printStackTrace(); + } + System.exit(1); + } + +} diff --git a/src/main/java/manage/Data.java b/src/main/java/manage/Data.java new file mode 100644 index 0000000..3ed39cf --- /dev/null +++ b/src/main/java/manage/Data.java @@ -0,0 +1,22 @@ +package manage; + +import com.google.api.client.util.Key; + +import java.util.List; + +//da modificare inserendo gli attributi del json (activity, heartrate, sleep, location) + +public class Data { + + @Key + public String id; + + @Key + public List tags; + + @Key + public String title; + + @Key + public String url; +} \ No newline at end of file diff --git a/src/main/java/manage/FITBITUrl.java b/src/main/java/manage/FITBITUrl.java new file mode 100644 index 0000000..d3de6bb --- /dev/null +++ b/src/main/java/manage/FITBITUrl.java @@ -0,0 +1,28 @@ +package manage; + +import com.google.api.client.http.GenericUrl; +import com.google.api.client.util.Key; + +public class FITBITUrl extends GenericUrl { + + @Key + private String fields; + + public FITBITUrl(String encodedUrl) { + super(encodedUrl); + } + + /** + * @return the fields + */ + public String getFields() { + return fields; + } + + /** + * @param fields the fields to set + */ + public void setFields(String fields) { + this.fields = fields; + } +} diff --git a/src/main/java/manage/OAuth2ClientCredentials.java b/src/main/java/manage/OAuth2ClientCredentials.java new file mode 100644 index 0000000..5c66572 --- /dev/null +++ b/src/main/java/manage/OAuth2ClientCredentials.java @@ -0,0 +1,26 @@ +package manage; + +public class OAuth2ClientCredentials { + + /** Value of the "API Key". */ + public static final String API_KEY = "22CSTL"; + + /** Value of the "API Secret". */ + public static final String API_SECRET = "ea2452013abd35609940ce5601960a08"; + + /** Port in the "Callback URL". */ + public static final int PORT = 8080; + + /** Domain name in the "Callback URL". */ + public static final String DOMAIN = "http://127.0.0.1:8080/"; + + 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); + } + } + } + diff --git a/src/main/java/manage/UserData.java b/src/main/java/manage/UserData.java new file mode 100644 index 0000000..14e52cd --- /dev/null +++ b/src/main/java/manage/UserData.java @@ -0,0 +1,17 @@ +package manage; + + +import com.google.api.client.util.Key; +import java.util.List; + +public class UserData { + + @Key + public List list; +/* + @Key + public int limit; + + @Key("has more") + public boolean hasMore;*/ //don't think are needed +} \ No newline at end of file