目标:
我有一个简单的网络应用程序,我可以在其中提交我的血压和心率测量值。现在我想把这些测量结果提交给我的谷歌健身账户。
我试过谷歌教程:https://developers.google.com/fit/scenarios/write-bp-data
OAuth2设置
我在一个现有的云控制台项目中创建了新的凭据。然后使用以下范围请求一个新的授权代码(该项目处于测试状态,我的帐户注册为测试人员(:
- https://www.googleapis.com/auth/fitness.blood_pressure.write
- https://www.googleapis.com/auth/fitness.heart_rate.write
请求令牌返回以下响应:
{
"access_token": "ya29.XXXXXXXXXXXXXXXX",
"expires_in": 3599,
"refresh_token": "1//XXXXXXXXXXXXXXXX",
"scope": "https://www.googleapis.com/auth/fitness.heart_rate.write https://www.googleapis.com/auth/fitness.blood_pressure.write",
"token_type": "Bearer"
}
正如您所看到的,两个写作用域都被授予了权限。此外,如果我检查我的谷歌帐户中的权限,两个范围都已设置。
创建数据源
现在我创建了两个数据源(一个用于com.google.heart_rate.bpm,另一个用于com.ibm.google.blood_pressure(。创建心率数据源按预期工作,但创建血压数据源会返回403 PERMISSION_DENIED错误:
请求(POSThttps://www.googleapis.com/fitness/v1/users/me/dataSources)
{
"dataStreamName": "BPA-BloodPressure",
"type": "raw",
"application": {
"detailsUrl": "https://aaa.bbb.ccc",
"name": "Blood Pressure App",
"version": "1"
},
"dataType": {
"name": "com.google.blood_pressure"
}
}
响应
{
"error": {
"code": 403,
"message": "Scope not included to modify data of type com.google.blood_pressure. Possible scopes: https://www.googleapis.com/auth/fitness.blood_pressure.write",
"errors": [
{
"message": "Scope not included to modify data of type com.google.blood_pressure. Possible scopes: https://www.googleapis.com/auth/fitness.blood_pressure.write",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
Google Fit REST API告诉我,缺少Scopefitnesty.blood_pressure.write,但正如您所看到的,为使用的访问令牌提供了Scope。
票据
- 我正在与Postman合作测试所有请求。稍后将使用Java/Govy或PHP后端
- 我还尝试了两种数据类型的读和写作用域
- 我可以在Google Fit的";已连接的应用程序">
将dataType名称更改为"健身.血压;而不是";com.google.blood_pressure;
身体会是这样的。
{
"dataStreamName": "BPA-BloodPressure",
"type": "raw",
"application": {
"detailsUrl": "https://aaa.bbb.ccc",
"name": "Blood Pressure App",
"version": "1"
},
"dataType": {
"name": "fitness.blood_pressure"
}
}