Authorization on Android

User consent is always required before your app can read or write fitness data. To obtain authorization:

  • Register your Android app with a project in the Google Developers Console.
  • Specify a scope of access when connecting to the fitness service.

In Google Fit, scopes are strings that determine what kinds of fitness data an app can access and the level of access to this data.

Figure 1: The consent screen.

Authorization flow

The authorization flow is the following:

  1. Your app requests a connection, with one or more scopes of access, to the fitness service.
  2. Google Fit prompts the user to grant your app the required permissions.
  3. If the user consents, your app can access fitness data of the types defined by the scope.

The specific permissions requested from the user depend on the scopes that your app specifies when connecting to the service.

Fitness scopes

The scopes for Google Fit on Android are defined as public fields of the Scopes class. Their field names start with the FITNESS_ prefix. Each scope provides access to a set of fitness data types. Some scopes provide read-only access to fitness data, but other scopes provide read and write access to fitness data. The scopes are listed in table 1.

For an example on how to request particular scopes when connecting to the service, see Getting Started. For more information about data types, see Fitness Data Types.

Table 1: Scopes for Google Fit

Permission Scope Type of Access Data Types
Activity FITNESS_ACTIVITY_READ Read com.google.activity.sample
com.google.activity.segment
com.google.activity.summary
(deprecated) com.google.calories.consumed
com.google.calories.expended
com.google.cycling.pedaling.cadence
com.google.power.sample
com.google.step_count.cadence
com.google.step_count.delta
com.google.activity.exercise
FITNESS_ACTIVITY_READ_WRITE Read and Write
Body FITNESS_BODY_READ Read com.google.heart_rate.bpm
com.google.heart_rate.summary
com.google.height
com.google.weight
com.google.weight.summary
FITNESS_BODY_READ_WRITE Read and Write
Location FITNESS_LOCATION_READ Read com.google.cycling.wheel_revolution.cumulative
com.google.cycling.wheel.revolutions
com.google.distance.delta
com.google.location.sample
com.google.location.bounding_box
com.google.speed
com.google.speed.summary
FITNESS_LOCATION_READ_WRITE Read and Write
Nutrition FITNESS_NUTRITION_READ Read com.google.nutrition.item
com.google.nutrition.summary
FITNESS_NUTRITION_READ_WRITE Read and Write

Scopes for Google Fit with Android 6.0 Marshmallow

Android 6.0 changes the permissions model to provide increased visibility and control over individual permissions. On previous versions of Android, Google Fit uses OAuth permissions to allow apps to read location and sensor data from Google Fit, but permissions on Android 6.0 are managed at a more granular level.

Apps built with Google Fit use the ACCESS_FINE_LOCATION and BODY_SENSORS permissions. Apps that access one of the data types listed in the following table need to specify the required permissions in your app's AndroidManifest.xml:

Table 2: Scopes for Google Fit with Android 6.0 Marshmallow

Scope Data Types
ACCESS_FINE_LOCATION com.google.location.sample
ACCESS_FINE_LOCATION com.google.location.bounding_box
ACCESS_FINE_LOCATION com.google.location.speed
ACCESS_FINE_LOCATION com.google.location.speed_summary
ACCESS_FINE_LOCATION com.google.distance.delta
BODY_SENSORS com.google.heart_rate.bpm
BODY_SENSORS com.google.heart_rate.summary

Apps built with various versions of the Google Play services client library behave as follows:

  • If the Google Play services client library version is greater than 8.1, the app throws a security exception if a required runtime permission is missing from AndroidManifest.xml.
  • If the Google Play services client library version is less than or equal to 8.1, your app continues to behave as it has, but Google recommends updating AndroidManifest.xml so that users are aware of the data used by your app.

Devices running an Android version less than 6.0 silently fail if a required permission is not requested in the AndroidManifest.xml file, regardless of whether your app targets Android API level 23 or not.

发送以下问题的反馈:

此网页
Google Fit
Google Fit