首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将点保存到Google Fitness API (fitness.body.write)

将点保存到Google Fitness API (fitness.body.write)
EN

Stack Overflow用户
提问于 2015-01-13 04:18:13
回答 2查看 859关注 0票数 1

我正在尝试将一个带浮点值的点保存到fitness.body中。获取值不是问题,而保存新点会导致403. No permission to modify data for this source.

我正在使用DataSetId derived:com.google.weight:com.google.android.gms:merge_weight查找点和读取值,并使用raw:com.google.weight:com.google.android.apps.fitness:user_input插入数据。

这是一个使用Ruby和google-api-ruby-client的工作流

代码语言:javascript
复制
require 'google/api_client'
require 'google/api_client/client_secrets'
require 'google/api_client/auth/installed_app'
require 'pry'

# Initialize the client.
client = Google::APIClient.new(
  :application_name => 'Example Ruby application',
  :application_version => '1.0.0'
)

fitness = client.discovered_api('fitness')

# Load client secrets from your client_secrets.json.
client_secrets = Google::APIClient::ClientSecrets.load

flow = Google::APIClient::InstalledAppFlow.new(
  :client_id => client_secrets.client_id,
  :client_secret => client_secrets.client_secret,
  :scope => ['https://www.googleapis.com/auth/fitness.body.write',
             'https://www.googleapis.com/auth/fitness.activity.write',
             'https://www.googleapis.com/auth/fitness.location.write']
)
client.authorization = flow.authorize

形成我的新数据点:

代码语言:javascript
复制
dataSourceId = 'raw:com.google.weight:com.google.android.apps.fitness:user_input'

startTime = (Time.now-1).to_i # 1 Second ago
endTime = (Time.now).to_i

metadata = {
  dataSourceId: dataSourceId,
  maxEndTimeNs: "#{startTime}000000000", # Faking nanoseconds with tailing zeros
  minStartTimeNs: "#{endTime}000000000",
  point: [
    {
      endTimeNanos: "#{endTime}000000000",
      startTimeNanos: "#{startTime}000000000",
      value: [
        { fpVal: 80 }
      ]
    }
  ]
}

正在尝试保存点:

代码语言:javascript
复制
result = client.execute(
  :api_method => fitness.users.data_sources.datasets.patch,
  :body_object => metadata,
  :parameters => {
    'userId' => "me",
    'dataSourceId' => dataSourceId,
    'datasetId' => "#{Time.now.to_i-1}000000000-#{(Time.now).to_i}000000000"
  }
)

正如我之前指出的,我得到了403. No permission to modify data for this source

代码语言:javascript
复制
#<Google::APIClient::Schema::Fitness::V1::Dataset:0x3fe78c258f60 DATA:{"error"=>{"er
rors"=>[{"domain"=>"global", "reason"=>"forbidden", "message"=>"No permission to modif
y data for this source."}], "code"=>403, "message"=>"No permission to modify data for
this source."}}>

我相信,我在作用域中选择了所有必需的权限。我尝试将该点提交给fitness.body的两个可访问数据集I。

如果我做错了什么,请让我知道。

谢谢!

EN

回答 2

Stack Overflow用户

发布于 2015-02-13 05:48:02

尝试添加Authorization标头:

代码语言:javascript
复制
result = client.execute(
  :api_method => fitness.users.data_sources.datasets.patch,
  :headers => {'Authorization' => 'Bearer YOUR_AUTH_TOKEN'},
  :body_object => metadata,
  :parameters => {
    'userId' => "me",
    'dataSourceId' => dataSourceId,
    'datasetId' => "#{Time.now.to_i-1}000000000-#{(Time.now).to_i}000000000"
  }
)
票数 0
EN

Stack Overflow用户

发布于 2017-12-21 05:29:42

我遇到了同样的情况,原来您不能将数据点直接插入到数据源"raw:com.google.weight:com.google.android.apps.fitness:user_input".中从名称上看,人们可能会猜到这个数据源是保留的。因此,解决方法是添加您自己的数据源,注意应使用dataType.name="com.google.weight",如下所示:

代码语言:javascript
复制
{
  "dataStreamName": "xxxx.body.weight", 
  "dataType": {
    "field": [
      {
        "name": "weight", 
        "format": "floatPoint"
      }
    ], 
    "name": "com.google.weight"
  }, 
  "dataQualityStandard": [], 
  "application": {
    "version": "1", 
    "name": "Foo Example App", 
    "detailsUrl": "http://example.com"
  }, 
  "device": {
    "model": "xxxmodel", 
    "version": "1", 
    "type": "scale", 
    "uid": "xxx@yyy", 
    "manufacturer": "xxxxManufacturer"
  }, 
  "type": "derived"
}

创建成功后,您可以使用此数据源(数据流id)插入您自己的数据点,然后当您使用后缀"dataPointChanges“进行查询时,插入的数据点也会包含在数据源"derived:com.google.weight:com.google.android.gms:merge_weight”中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27910068

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档