繁体   English   中英

Google通过Google Python API库拟合数据

[英]Google fit data via google python api libraries

我正在使用Google提供的这个python库 ,但无法弄清楚“ body”参数要使用什么。 我可以借鉴一个示例主体来创建此工具需要的命令吗?

这是我正在使用的代码:

flow = client.flow_from_clientsecrets(
    workLaptop,
    scope='https://www.googleapis.com/auth/fitness.activity.read',
    redirect_uri='oauth:code:from:somehwere')

auth_uri = flow.step1_get_authorize_url()
webbrowser.open_new(auth_uri)

auth_code = "a;ldskjfa;lsdkfja;ldsfkja;lsdkfjaldgha;"

credentials = flow.step2_exchange(auth_code)
http_auth = credentials.authorize(httplib2.Http())

service = discovery.build('fitness', 'v1',http_auth)
fitData = service.users().dataset().aggregate(userId='me',body=body).execute()

直到我需要定义身体的那一部分都很好。 这是我要尝试的身体:

body = {
    "aggregateBy": [
      {
        "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps",
        "dataTypeName": "com.google.step_count.delta" 
      },
    ],
    "bucketByActivitySegment": {
      "minDurationMillis": "A String", # Only activity segments of duration longer than this is used
    },
    "endTimeMillis": "1435269600000000000",
    "bucketBySession": {
      "minDurationMillis": "10", # Only sessions of duration longer than this is used
    },
    "bucketByActivityType": {
      "minDurationMillis": "10", # Only activity segments of duration longer than this is used
    },
    "startTimeMillis": "1435183200000000000", # required time range
    "bucketByTime": { # apparently oneof is not supported by reduced_nano_proto
      "durationMillis": "10",
    },
}

我的身体命令有什么问题? 这是错误代码:https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate?alt=json返回了“内部错误“>

这是API资源管理器中对象的示例: 在此处输入图片说明

尽管我不是100%同意使用Google Fit的Google API,但在最初的实例中,您的JSON正文请求肯定存在一些问题。

例如:

    body = {
    "aggregateBy": [
      {
        "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps",
        "dataTypeName": "com.google.step_count.delta" 
      },
    ],
    "bucketByActivitySegment": {
      "minDurationMillis": "A String", # Only activity segments of duration longer than this is used
    },
    "endTimeMillis": "1435269600000000000",
    "bucketBySession": {
      "minDurationMillis": "10", # Only sessions of duration longer than this is used
    },
    "bucketByActivityType": {
      "minDurationMillis": "10", # Only activity segments of duration longer than this is used
    },
    "startTimeMillis": "1435183200000000000", # required time range
    "bucketByTime": { # apparently oneof is not supported by reduced_nano_proto
      "durationMillis": "10",
    },
}

实际上应该是这样;

    body = {
    "aggregateBy": [
      {
        "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps",
        "dataTypeName": "com.google.step_count.delta" 
      }
    ],
    "bucketByActivitySegment": {
      "minDurationMillis": "A String" # Only activity segments of duration longer than this is used
    },
    "endTimeMillis": "1435269600000000000",
    "bucketBySession": {
      "minDurationMillis": "10" # Only sessions of duration longer than this is used
    },
    "bucketByActivityType": {
      "minDurationMillis": "10" # Only activity segments of duration longer than this is used
    },
    "startTimeMillis": "1435183200000000000", # required time range
    "bucketByTime": { # apparently oneof is not supported by reduced_nano_proto
      "durationMillis": "10"
    }
}

基于JSON的rest服务对于在不应该使用的多余逗号的情况下确实不能原谅,它使字符串无法jsonable,这将导致500错误。 首先尝试一下;)

我自己不是专家,但是我已经使用API​​数天了。 这是我的OAuth运动场的示例。

样品回复

据我了解,您的“ endTimeMillis”:“ 1435269600000000000”未正确定义,因为它以纳秒为单位。 要将其更改为毫米,请将其更改为“ 1435269600000”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM