调用Google Fit睡眠数据读取API返回400 Bad Request错误求助
Let’s break down why you’re hitting that 400 Bad Request error and fix it step by step:
1. Incorrect Timestamp Format (Most Likely Culprit)
Your startTime parameter has a formatting mistake: 2019-12-05T00:00.000Z is missing a colon between the minutes and seconds. The Google Fitness API strictly follows RFC 3339 timestamp standards, which require the structure YYYY-MM-DDTHH:mm:ss.sssZ.
Corrected startTime: 2019-12-05T00:00:00.000Z
2. Verify Your Scope and Access Token
While https://www.googleapis.com/auth/fitness.activity.read does cover sleep activity (since activityType=72 maps to sleep), double-check two things:
- You explicitly requested this scope during the OAuth authorization flow (including in Google Playground).
- Your access token is valid and includes this scope. You can decode the token (it’s a JWT) to confirm the scopes are present in the
scopeclaim.
3. Corrected Request Example
Here’s the fixed version of your HTTP request with proper formatting:
GET /fitness/v1/users/me/sessions?startTime=2019-12-05T00:00:00.000Z&endTime=2019-12-17T23:59:59.999Z&activityType=72 HTTP/1.1 Host: www.googleapis.com Authorization: Bearer YOUR_ACTUAL_ACCESS_TOKEN Content-Length: 0
Just replace YOUR_ACTUAL_ACCESS_TOKEN with your valid token, and ensure there are no typos in parameter names or values.
4. Quick Additional Checks
- Confirm the Fitness API is enabled in your Google Cloud project (you can check this in the Cloud Console under APIs & Services > Enabled APIs & Services).
- If using Google Playground, make sure you’ve completed the full OAuth flow, including granting the app permission to access your fitness data.
内容的提问来源于stack exchange,提问作者Payal Pandey

