Authorization and permission scopes

User authorization is required before your application can read or write any fitness sensor data. User authorization is a two-step process. Step one is the registration of your application with the Google developer console, which is done outside your application. Step two is getting authorization from the user by using relevant scopes inside your application.

Registration with the Google developer console

Every application that needs to access fitness sensor data needs to register with the Google developer console. The followings steps explain the registration process:

  1. Open the Google developer console in any browser.
  2. Create a project from the console and enter your project name, which could be the same or different from your application name.
  3. Find the Fitness API from the APIs and Auth console menu and turn it on. Now Fitness API should appear at the top of your API list.
  4. Go to the Credential console menu and click on Create a new Client ID. This will open a new pop-up window to enter your application details into.
  5. In the pop-up window, select your application type as Android and give the name of your application.
  6. Provide the SHA1 fingerprint of your signing certificate in the relevant box of the pop-up window.
  7. Provide the application package name from your manifest file and click on the Create Client ID button to complete the process.

Authorization from a user in the application

After completing the one-time registration with Google developer console, the user consent has to be taken inside the application. As a first step, we have to select the required scopes and then we have to connect to Google Play Services. Depending on the number of scopes set, Google Play Services will prompt the user with an authorization dialog showing the required permissions. Once the user gives consent, your applications can access the Fitness APIs. The implementation details are discussed in the fitness tracker application, in the second half of the chapter. The following is a screenshot of the authorization dialog, showing the required permissions for the device:

Authorization from a user in the application

Fitness scopes

Scopes are strings that determine what kinds of fitness data an app can access and also define the level of access to this data. Scopes belong to a particular permissions group. The following table lists the different types of scope, their type of access, their data types, and the permissions group that they belong to.

Permission Group

Scopes

Type of Access

Data Types

Activity

FITNESS_ACTIVITY_READ

Read

com.google.activity.sample

   

com.google.activity.segment

   

com.google.activity.summary

   

com.google.calories.consumed (deprecated)

   

com.google.calories.expended

 

FITNESS_ACTIVITY_READ_WRITE

Read and write

com.google.cycling.pedaling.cadence

   

com.google.power.sample

   

com.google.step_count.cadence

   

com.google.step_count.delta

   

com.google.activity.exercise

Body

FITNESS_BODY_READ

Read

com.google.heart_rate.bpm

   

com.google.heart_rate.summary

 

FITNESS_BODY_READ_WRITE

Read and write

com.google.height

   

com.google.weight

   

com.google.weight.summary

Location

FITNESS_LOCATION_READ

Read

com.google.cycling.wheel_revolution.cumulative

   

com.google.cycling.wheel.revolutions

   

com.google.distance.delta

 

FITNESS_LOCATION_READ_WRITE

Read and write

com.google.location.sample

   

com.google.location.bounding_box

   

com.google.speed

   

com.google.speed.summary

Nutrition

FITNESS_NUTRITION_READ

Read

com.google.nutrition.item

 

FITNESS_NUTRITION_READ_WRITE

Read and write

com.google.nutrition.summary

Installing and running time permissions

If your application is using any of the DataTypes that belong to either the Location or Body permission groups shown in the preceding table, your application needs to have the following permissions:

  • Install time permissions in AndroidManifest.xml: If your application uses a DataType that belong to either the Location or Body permission groups, then it needs to have ACCESS_FINE_LOCATION for Location-based data types and BODY_SENSORS for body-based data types.
  • Runtime permissions: If your application uses a DataType that belong to either the Location or Body permission groups and is compiled with SDK version 23 (Marshmallow) or above, then your application needs to get run time permission for ACCESS_FINE_LOCATION and BODY_SENSORS.

The installation time permissions in AndroidManifest.xml and the run time permissions are different from the user authorization discussed in the previous section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset