I want to write a java program using the Google Fit REST API. (It's not an Android app, so I can't use the Android API.)
It seems like there is a Java Client library for the REST API - so I was thinking this would be easy.
However, I can't find any samples/examples that explain how to use the client library (just the javadoc).
I found examples about how to use the Android API - e.g. this. But it's completely different, so no use.
For other Google APIs that I have used, the Client Library comes with examples... not so in this case.
Any ideas please? Thanks.
By piecing together some information from the javadoc, the REST API documentation, and by using some of my own examples from using other Google API Java Client Libraries, I have been able to make it work more or less... Here are some code snippets:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.services.fitness.Fitness;
import com.google.api.services.fitness.model.*;
Get a GoogleCredential - this library is well documented...
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
...
.build();
Create the service that is the basis for all the calls, using credential
just created...
Fitness service = new Fitness.Builder(
HTTP_TRANSPORT,
JSON_FACTORY,
credential)
.setApplicationName(APPLICATION_NAME)
.build();
Create one or more data sources for your application / device
DataSource content = new DataSource()
.setApplication(YOUR_APPLICATION)
.setType(DATA_SOURCE_RAW)
.setName(name)
.setDataType(dataType);
Fitness.Users.DataSources.Create request =
service.users().dataSources().create("me", content);
DataSource ds = request.execute();
This is an example of a dataType
that you need to use when you create your own DataSource
... This is using a standard, public metric (in this case steps) that Google exposes... see this.
private static final String DT_STEPS = "com.google.step_count.delta";
private static final DataTypeField FIELD_STEPS = new
DataTypeField().setName("steps").setFormat(FORMAT_INTEGER);
private static final List<DataTypeField> STEPS_FIELDS = Arrays.asList(
FIELD_STEPS);
public static final DataType DATA_TYPE_STEPS =
new DataType()
.setName(DT_STEPS)
.setField(STEPS_FIELDS);
Now create the actual data points, of that DataType
, using that DataSource
- in a DataSet
...
Value value = new Value().setIntVal(Integer.valueOf(steps));
List<Value> values = Arrays.asList(value);
DataPoint point = new DataPoint()
.setValue(values)
.setDataTypeName(DT_STEPS)
.setStartTimeNanos(startNS)
.setEndTimeNanos(endNS);
List<DataPoint> dataPoints = Arrays.asList(point);
String datasetId = startNS + "-" endNS;
Dataset content = new Dataset()
.setDataSourceId(dataSourceId)
.setPoint(dataPoints)
.setMinStartTimeNs(startNS)
.setMaxEndTimeNs(endNS);
Fitness.Users.DataSources.Datasets.Patch request =
service.users().dataSources().datasets().patch(
"me",
dataSourceId,
datasetId,
content);
Dataset ds = request.execute();
You still need to create a Session
and a Segment Activity DataSet
... but those follow similar structures.