Added classes for manage authentication and retrieve data to/from fitbit... but the code needs some mods!
This commit is contained in:
committed by
Dawit Gulino 20013954
parent
b79a7615d2
commit
7d28391db0
@@ -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'
|
||||
|
||||
|
||||
}
|
||||
|
||||
116
src/main/java/manage/AuthFITBIT.java
Normal file
116
src/main/java/manage/AuthFITBIT.java
Normal file
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
22
src/main/java/manage/Data.java
Normal file
22
src/main/java/manage/Data.java
Normal file
@@ -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<String> tags;
|
||||
|
||||
@Key
|
||||
public String title;
|
||||
|
||||
@Key
|
||||
public String url;
|
||||
}
|
||||
28
src/main/java/manage/FITBITUrl.java
Normal file
28
src/main/java/manage/FITBITUrl.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
26
src/main/java/manage/OAuth2ClientCredentials.java
Normal file
26
src/main/java/manage/OAuth2ClientCredentials.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
17
src/main/java/manage/UserData.java
Normal file
17
src/main/java/manage/UserData.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package manage;
|
||||
|
||||
|
||||
import com.google.api.client.util.Key;
|
||||
import java.util.List;
|
||||
|
||||
public class UserData {
|
||||
|
||||
@Key
|
||||
public List<Data> list;
|
||||
/*
|
||||
@Key
|
||||
public int limit;
|
||||
|
||||
@Key("has more")
|
||||
public boolean hasMore;*/ //don't think are needed
|
||||
}
|
||||
Reference in New Issue
Block a user