This tutorial shows you how to activate access to the Fitness REST API, obtain OAuth access tokens, and invoke the API methods using HTTP requests.
To use the Fitness REST API, it is important that you understand the basics of RESTful web services and JSON representations .
Get a Google Account
To use the Fitness REST API, you need a Google Account . If you already have an account, then you're all set. You may also want to create a separate Google Account for testing purposes.
Request an OAuth 2.0 client ID
Follow these steps to request an OAuth 2.0 client ID for the Fitness API.
- Go to the Google API Console .
- Select a project, or create a new one. Use the same project for the Android and REST versions of your app.
- Click Continueto enable the Fitness API.
- Click Go to credentials.
- Click New credentials, then select OAuth Client ID.
- Under Application type, select Web application.
- Under Authorized JavaScript origins, enter the base URL of the site
from which requests will originate (for example
https://developers.google.com
is the URL used by the OAuth Playground). - Under Authorized redirect URI, enter the URL of the site where responses
will be handled (for example
https://developers.google.com/oauthplayground
is the URL used by the OAuth Playground). -
Click Create. Your new OAuth 2.0 Client ID and secret appear in the list of IDs for your project. An OAuth 2.0 Client ID is a string of characters, something like this:
780816631155-gbvyo1o7r2pn95qc4ei9d61io4uh48hl.apps.googleusercontent.com
Try the REST API in the OAuth Playground
The OAuth Playground is the easiest way to familiarize yourself with the Fitness REST API by submitting HTTP requests and observing the responses before you write any client code.
To authorize the Fitness REST API in the OAuth Playground:
- Go to the OAuth Playground .
- Under Step 1 Select & authorize APIs, expand Fitness v1and select the Fitness scopes to use.
- Click the Authorize APIsbutton, select the Google API Console project to use, and click Allowwhen prompted. You will be able to access and modify data associated with the selected Google API Console account.
- Click the Exchange authorization code for tokensbutton. The OAuth
Playground automatically includes this header in the
Authorization:
request header when you submit HTTP requests. Note that the access token will expire after 60 minutes (3600 seconds).
Submit HTTP requests
The following examples demonstrate how to send HTTP requests to list all available data sources, and to create a new data source. For the Fitness REST API, the URI format is:
https://www.googleapis.com/fitness/v1/ resourcePath
? parameters
To list all available data sources:
- In HTTP Method, select GET.
- In Request URI, enter
https://www.googleapis.com/fitness/v1/users/me/dataSources
- Click Send the request.
The request and the response appear on the right side of the page. If the request is successful, the response shows the data source from the previous example in JSON format.
To create a data source:
- In HTTP Method, select POST.
- In Request URI, enter
https://www.googleapis.com/fitness/v1/users/me/dataSources
- Click Enter request body.
-
In the Request Bodywindow, copy and paste the following JSON:
{ "dataStreamName" : "MyDataSource" , "type" : "derived" , "application" : { "detailsUrl" : "http://example.com" , "name" : "Foo Example App" , "version" : "1" }, "dataType" : { "field" : [ { "name" : "steps" , "format" : "integer" } ], "name" : "com.google.step_count.delta" }, "device" : { "manufacturer" : "Example Manufacturer" , "model" : "ExampleTablet" , "type" : "tablet" , "uid" : "1000001" , "version" : "1" } }
-
In the Request Bodywindow, click Close.
-
Click Send the request.
The request and the response appear on the right side of the page. The request
includes the OAuth access token in the Authorization
header:
Authorization: Bearer ya29. OAuthTokenValue
If the request is successful, the first line of the response is:
HTTP/1.1 200 OK
Use cURL to access the Fit REST API
You can use the cURL command line tool to access the Fit REST API. You will need an OAuth access token to make requests using cURL (see the preceding instructions ). Note that access tokens expire after an hour. The following example shows a simple bash script to list all data sources.
#!/bin/bash ACCESS_TOKEN = "" curl \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $ACCESS_TOKEN " \ https://www.googleapis.com/fitness/v1/users/me/dataSources
Next steps
To learn more about the REST API, see these pages: