我试图用Python调用Google Fit API,但会话示例不适合我。google提供的示例:
GET https://fitness.googleapis.com/fitness/v1/users/me/sessions?activityType=97 HTTP/1.1
Authorization: Bearer [YOUR_ACCESS_TOKEN]
Accept: application/json
我的代码@api_view(['GET'])
def googleFitView(request):
social_token = SocialToken.objects.get(account__user=2)
token=social_token.token
url_session= "https://fitness.googleapis.com/fitness/v1/users/me/sessions"
headers = {
"Authorization": "Bearer {}".format(token),
"Accept": "application/json"
}
session_call = requests.post(url_session, headers=headers)
return Response(session_call)
如果我调用Google Fit的另一个API,它正在工作。因此,身份验证不是问题。我遵循了文档,调用会话端点不需要正文
@api_view(['GET'])
def googleFitView(request):
social_token = SocialToken.objects.get(account__user=2)
token=social_token.token
url = "https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate"
headers = {
"Authorization": "Bearer {}".format(token),
"Content-Type": "application/json;encoding=utf-8",
}
body = {
"aggregateBy": [{
"dataTypeName": "com.google.activity.segment",
}],
"startTimeMillis": 1634767200000,
"endTimeMillis": 1634853600000
}
respo = requests.post(url, data=json.dumps(body), headers=headers)
return Response(respo)
看起来你想做一个GET调用,但你在下面的方法googleFitView
中调用POST
@api_view(['GET'])
def googleFitView(request):
social_token = SocialToken.objects.get(account__user=2)
token=social_token.token
url_session= "https://fitness.googleapis.com/fitness/v1/users/me/sessions"
headers = {
"Authorization": "Bearer {}".format(token),
"Accept": "application/json"
}
# session_call = requests.post(url_session, headers=headers) -- This is a post call.
session_call = requests.get(url_session, headers=headers)
return Response(session_call)