Getting Started with the REST API

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.

  1. Go to the Google API Console.
  2. Select a project, or create a new one. Use the same project for the Android and REST versions of your app.
  3. Click Continue to enable the Fitness API.
  4. Click Go to credentials.
  5. Click New credentials, then select OAuth Client ID.
  6. Under Application type, select Web application.
  7. 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).
  8. 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).
  9. 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:

  1. Go to the OAuth Playground.
  2. Under Step 1 Select & authorize APIs, expand Fitness v1 and select the Fitness scopes to use.
  3. Click the Authorize APIs button, select the Google API Console project to use, and click Allow when prompted. You will be able to access and modify data associated with the selected Google API Console account.
  4. Click the Exchange authorization code for tokens button. 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:

  1. In HTTP Method, select GET.
  2. In Request URI, enter https://www.googleapis.com/fitness/v1/users/me/dataSources
  3. 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:

  1. In HTTP Method, select POST.
  2. In Request URI, enter https://www.googleapis.com/fitness/v1/users/me/dataSources
  3. Click Enter request body.
  4. In the Request Body window, 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"
      }
    }
    
    
  5. In the Request Body window, click Close.

  6. 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:

Send feedback about...