【问题标题】:Saving Point to a Google Fitness API (fitness.body.write)将点保存到 Google Fitness API (fitness.body.write)
【发布时间】:2017-12-21 05:29:41
【问题描述】:

我正在尝试将具有浮点值的 Point 保存到 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 的工作流程:

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

形成我的新数据点:

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 }
      ]
    }
  ]
}

试图保存点:

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

#<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."}}>

我相信,我在范围内选择了所有必需的权限。我尝试将这个点提交给 fitit.body 的两个可访问的 datasetid。

如果我在这里做错了什么,请告诉我。

谢谢!

【问题讨论】:

  • userId,我经过身份验证了吗?您是否在标头中发送身份验证令牌?

标签: ruby google-api google-api-client google-fit google-fit-sdk


【解决方案1】:

尝试添加授权标头:

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"
  }
)

【讨论】:

    【解决方案2】:

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

    {
      "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"
    }
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多