使用Python访问googlefitrestapi

2024-04-19 06:04:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在服务器上使用googlefitrestapi并对Fit数据执行CRUD操作。用户已通过身份验证,https://www.googleapis.com/auth/fitness.activity.write,https://www.googleapis.com/auth/fitness.body.read,https://www.googleapis.com/auth/fitness.body.write的权限已被授予应用程序的fit数据。用户通过身份验证后,服务器将接收UID和OAUTH_令牌。在

UID = "XYZ"
OAUTH_TOKEN="FOO_BAR"
APP_SECRET_KEY = "XXX"
CLIENT_ID = "AAA"

但每当我在restapi上获取/发布时,它就会抛出401错误。在

^{pr2}$

输出

401
{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

这应该列出https://developers.google.com/fit/rest/v1/get-started中提到的所有可用数据源。在

这里怎么了?在


Tags: 数据用户https服务器comauth身份验证uid
1条回答
网友
1楼 · 发布于 2024-04-19 06:04:21

花了许多小时之后,问题的解决办法如下。在

UserId应该是me,access_token应该是最新的,因为access token将在60分钟内过期。在

UID = "XYZ"
OAUTH_TOKEN="FOO_BAR"
APP_SECRET_KEY = "XXX"
CLIENT_ID = "AAA"

import requests
url = "https://www.googleapis.com/fitness/v1/users/me/dataSources"

headers = { 'content-type': 'application/json',
            'Authorization': 'Bearer %s' % OAUTH_TOKEN }
r = requests.get(url, headers=headers)

print r.status_code
print r.content

相关问题 更多 >