Google Health API 是一个从头开始构建的综合解决方案,可让开发者可靠地访问各种经用户同意的健康数据和多样化的数据类型。Google Health API 采用新控制台来注册应用,并使用 Google OAuth 2.0、新数据类型、新端点架构和新响应格式。
本指南旨在帮助开发者将其现有的 Fitbit Web API 应用迁移到新的 Google Health API。其中包含一些建议,可确保在留住用户的同时实现无缝迁移。
为什么应该进行迁移?
这不仅仅是一次更新,更是一项战略举措,旨在确保您的应用安全可靠,并为未来健康技术的进步做好准备。使用 Google Health API 的一些好处包括:
- 访问全面的数据:获得对各种已获用户同意的健康数据和多样化数据类型的强大访问权限。
- 增强的安全性:遵守 Google 的安全最佳实践,符合 Google 的安全、隐私和身份标准。
- 一致性:消除了数据格式、时区、度量单位和错误处理方面的旧有不一致问题,从而提供更直观的开发者体验。
- 可伸缩性和前瞻性:旨在扩缩以满足未来需求,并支持 gRPC 等现代协议。
从 Fitbit Web API 过渡到 Google Health API 不仅涉及技术修改,由于改用了新的 OAuth 库,现有的访问令牌和刷新令牌无法转移,因此用户需要重新同意更新后的集成。
支持两种登录方法
由于 Fitbit Web API 和 Google Health API 使用不同的系统来处理用户登录,因此在 Fitbit Web API 仍处于活跃状态期间,您的应用需要暂时支持这两种身份验证方法,以满足不同用户群的需求。
不要让应用直接请求数据,而是实现一个层,由该层决定是针对特定用户与 Fitbit Web API 还是 Google Health API 通信,这样应用的其余部分就无需担心细节。
更新用户数据库,添加一个标志(例如 oauth_type)来标识用户使用的登录系统。
- 对于新用户:自动为其设置新的 Google Health API (
oauth_type: google)。 - 对于现有用户:在他们更新同意声明 (
oauth_type: fitbit) 之前,请让他们继续使用 Fitbit Web API。
| 用户场景 | oauth_type 州 |
活跃 API 流 | 所需操作 |
|---|---|---|---|
| 新用户引导 | google |
仅限 Google Health API | 在系统浏览器中启动 Google OAuth,并将 ID 与 users.getIdentity 映射。 |
| Fitbit 现有用户(尚未迁移) | fitbit |
仅限 Fitbit Web API | 继续与 Fitbit Web API 同步;显示可关闭的应用内升级提示。 |
| Fitbit 现有用户完成迁移 | fitbit → google |
切换到 Google Health API | 验证已授予的范围,调用 users.getIdentity(legacyUserId → healthUserId),将 oauth_type 切换为 google,并立即撤消旧版 Fitbit 令牌。 |
为避免干扰用户体验,我们建议您不要强制所有人退出并重新登录。相反:
- 当仍连接到 Fitbit Web API 的用户与您的应用互动时,向他们显示一条友好的通知,鼓励他们更新连接。
- 当用户接受更新操作时,立即触发 Google Health 登录流程。
- Google 登录成功后,将新的 Google 凭据保存到用户的个人资料中,将其
oauth_type标志从fitbit切换为google,并立即通过编程方式撤消其旧版 Fitbit 令牌来停用旧版同步,以防止重复的数据注入和双重关联。
确保数据连续性
将集成从旧版 Fitbit Web API 迁移到 Google Health API 时,开发者应用必须考虑用户标识结构的变化。
旧版 Fitbit Web API 使用 6 个字符的字母数字字符串(例如 A1B2C3)来标识账号,而 Google Health API 则使用 healthUserId,其格式为最多 63 位数字和字符的字符串。
为了在不丢失用户上下文的情况下弥合这一差距,开发者可以查询 getIdentity 端点,以获取 Fitbit 和 Health 用户 ID。
此端点会返回一个载荷,其中包含 legacyUserId 和新的 healthUserId,使应用能够在现有记录与新账号系统之间动态创建映射。
处理未关联的 Google 账号
用户必须登录 Google Health 移动应用,将 Google Health 与其 Google 账号相关联,然后您的应用才能通过 Google Health API 同步数据。请参阅在 OAuth 权限请求页面之前关联 Google Health。 由于 Google OAuth 2.0 会在用户同意期间对任何有效的 Google 账号进行身份验证,因此无法屏蔽尚未创建有效 Google Health 个人资料的账号。
如果用户在创建 Google Health 个人资料或将其旧版 Fitbit 账号迁移到 Google 账号之前完成了 Google OAuth 授权,则调用 users.getIdentity 或数据端点会返回 HTTP 400 Bad Request 错误,状态为 FAILED_PRECONDITION,原因为 ACCOUNT_NOT_LINKED:
{
"error": {
"code": 400,
"message": "The account is not linked to Google Health.",
"status": "FAILED_PRECONDITION",
"details": [
{
"@type": "type.googleapis.com/google.rpc.ErrorInfo",
"reason": "ACCOUNT_NOT_LINKED",
"domain": "health.googleapis.com",
"metadata": {
"redirect_uri": "https://fitbit.google.com/auth/signup"
}
}
]
}
}
如需验证账号访问权限并处理此错误,请执行以下操作:
- 用 OAuth 授权代码换取令牌,并立即调用
users.getIdentity(GET https://health.googleapis.com/v4/users/me/identity),然后再在应用界面中将账号标记为已关联。 - 如果
users.getIdentity或后台数据同步调用返回 HTTP400且原因为ACCOUNT_NOT_LINKED,请勿将账号标记为已关联。如果用户是从旧版 Fitbit Web API 迁移,请保持其现有 Fitbit 连接处于活跃状态,并且不要撤消其旧版 Fitbit 令牌。 显示一个可操作的提示,同时针对从未创建过 Google Health 或 Fitbit 账号的新用户,以及尚未迁移到 Google 账号的现有 Fitbit 用户:
“您的 Google 账号尚未与 Google Health 关联。打开 Google Health 移动应用,然后使用 Google 账号登录以创建个人资料,或将现有 Fitbit 账号迁移到 Google 账号,然后返回此处进行关联。”
提供一个行动号召按钮,用于从错误元数据 (
https://fitbit.google.com/auth/signup) 打开redirect_uri,或将旧版 Fitbit 用户引导至 Fitbit 账号迁移帮助文章。
回填历史数据
如果用户在旧版端点停用之前未向新版 Google Health API 端点进行身份验证,只要他们继续将设备同步到 Google Health 应用,其数据仍将可用。不过,您可能会遇到此用户的数据缺口。
如需回填其数据,请在用户重新向新端点进行身份验证后,使用我们的 Google Health API 回填其历史数据。如需相关指导,请参阅查询历史数据。
迁移和关联实现示例
以下参考实现演示了如何处理 OAuth 回调、验证部分同意或零同意、使用 users.getIdentity 解析用户身份(包括 HTTP 400 ACCOUNT_NOT_LINKED 处理)、通过撤消旧版 Fitbit 令牌来防止双重关联,以及使用 oauth_type 路由数据请求:
import requests
GOOGLE_HEALTH_IDENTITY_URL = "https://health.googleapis.com/v4/users/me/identity"
FITBIT_REVOKE_URL = "https://api.fitbit.com/oauth2/revoke"
DEFAULT_SIGNUP_URL = "https://fitbit.google.com/auth/signup"
def complete_google_health_linking(user_record: dict, oauth_token_response: dict, fitbit_client_creds: str) -> dict:
"""Completes Google Health account linking and migrates legacy Fitbit users."""
# 1. Check granted scopes (handle zero consent and partial consent)
granted_scopes = set(oauth_token_response.get("scope", "").split())
health_scopes = {s for s in granted_scopes if s.startswith("https://www.googleapis.com/auth/googlehealth")}
if not health_scopes:
return {
"status": "MISSING_PERMISSIONS",
"ui_action": "ROUTE_TO_MISSING_PERMISSIONS_SCREEN",
"message": "At least one permission is required to connect to Google Health.",
}
access_token = oauth_token_response["access_token"]
# 2. Call users.getIdentity and handle HTTP 400
# (status: "FAILED_PRECONDITION", ErrorInfo.reason: "ACCOUNT_NOT_LINKED").
identity_resp = requests.get(
GOOGLE_HEALTH_IDENTITY_URL,
headers={"Authorization": f"Bearer {access_token}", "Accept": "application/json"},
timeout=10,
)
if identity_resp.status_code == 400:
error_payload = identity_resp.json().get("error", {})
if error_payload.get("status") == "FAILED_PRECONDITION":
for detail in error_payload.get("details", []):
if detail.get("reason") == "ACCOUNT_NOT_LINKED":
redirect_uri = detail.get("metadata", {}).get("redirect_uri", DEFAULT_SIGNUP_URL)
return {
"status": "ACCOUNT_NOT_LINKED",
"ui_action": "PROMPT_LINK_GOOGLE_HEALTH_ACCOUNT",
"redirect_uri": redirect_uri,
"message": (
"Your Google Account isn't linked to Google Health yet. "
"Open the Google Health mobile app and sign in with your Google Account "
"to create a profile, or move your existing Fitbit account to your "
"Google Account, then return here to connect."
),
}
identity_resp.raise_for_status()
identity = identity_resp.json()
# 3. Verify legacyUserId and map legacyUserId -> healthUserId.
previous_oauth_type = user_record.get("oauth_type")
legacy_fitbit_refresh_token = user_record.get("fitbit_refresh_token")
existing_legacy_id = user_record.get("legacy_user_id")
returned_legacy_id = identity.get("legacyUserId")
legacy_id_verified = bool(returned_legacy_id) and (
not existing_legacy_id or returned_legacy_id == existing_legacy_id
)
if previous_oauth_type == "fitbit" and not legacy_id_verified:
return {
"status": "LEGACY_ID_MISMATCH",
"ui_action": "PROMPT_VERIFY_FITBIT_MIGRATION_ACCOUNT",
"message": (
"The Google Account you signed in with does not match your linked "
"Fitbit account. Sign in with the Google Account linked to your "
"Fitbit profile."
),
}
# 4. Cut over oauth_type and revoke legacy Fitbit token once verified.
user_record.update({
"health_user_id": identity.get("healthUserId") or user_record.get("health_user_id"),
"legacy_user_id": returned_legacy_id or existing_legacy_id,
"google_access_token": access_token,
"google_refresh_token": oauth_token_response.get("refresh_token"),
"granted_scopes": list(health_scopes),
"oauth_type": "google",
"fitbit_access_token": None,
"fitbit_refresh_token": None,
})
if previous_oauth_type == "fitbit" and legacy_fitbit_refresh_token:
requests.post(
FITBIT_REVOKE_URL,
headers={
"Authorization": f"Basic {fitbit_client_creds}",
"Content-Type": "application/x-www-form-urlencoded",
},
data={"token": legacy_fitbit_refresh_token},
timeout=10,
)
return {
"status": "CONNECTED",
"health_user_id": user_record["health_user_id"],
"granted_scopes": user_record["granted_scopes"],
}
沟通和时间安排
为帮助用户从现有的 Fitbit OAuth 迁移到新的 Google OAuth,请遵循以下最佳实践。
价值优先的沟通
不要以“我们更新了 API”开头,而应以“将 Google Health 数据集成到应用中可带来哪些好处”开头,但要确保用户知道,如果他们希望同步数据,则需要重新进行身份验证:
- 清楚地说明您的应用中提供了哪些由集成提供支持的功能,并根据用户如何从这些功能中受益来定制您的消息。
- 重点介绍您的功能并提供应用场景,而不是技术实现细节。
- 请勿说:“您将无法连接到 Fitbit API。”
- 建议:“如需继续查看包含心率数据的详细锻炼信息,请重新同意 Google Health API 的相关条款。”
何时通知用户
在所有用户通信中,请遵循 Google Health 品牌规范,并使用可关闭的横幅、卡片或提醒。
- 当用户正在锻炼或手动记录内容时,请勿触发重新征求权限请求页面。
- 只有在发出数周警告后,才强制要求重新征求同意,这与 Fitbit Web API 的官方弃用截止日期一致。
- 如果用户在硬性截止日期之后仍未重新同意,请提供一个平稳的恢复路径。在横幅、卡片或提示中提供帮助消息,帮助用户了解数据缺失的原因以及解决方法。
规划 OAuth 令牌授予速率限制
当现有 Fitbit 用户重新同意 Google Health API 时,每次用户授权都会计入 Google Cloud 项目的 OAuth 令牌授予速率限制。此限制控制每分钟和每 24 小时滑动窗口内可以授权您的应用的用户数量。
如果您预计在迁移期间,24 小时滑动窗口内的用户注册或重新身份验证次数会超过 10,000 次,请按照以下步骤操作,以免达到 OAuth 速率限制:
- 在 Google Cloud 云项目中监控令牌授予使用情况。
- 控制应用内迁移通知的发送频率,以免超出每日授权限额。
- 在提示大量用户重新进行身份验证之前,请先申请更高的 OAuth 令牌授予配额。如需了解如何查看用量和申请提高限额,请参阅 OAuth 应用速率限制。