Endpoint
GET /api/v1/pulse/theta/{provider_slug}/link
Overview
发起 OAuth 认证流程,用于连接用户的健康设备账号。该 endpoint 会返回一个授权 URL,用户需要访问该 URL 并授予权限。
Request
provider slug(例如 theta_garmin、theta_whoop)
curl "http://localhost:18080/api/v1/pulse/theta/theta_garmin/link?user_id=user_123&return_url=https://myapp.com/success"
Response
{
"link_web_url": "https://connect.garmin.com/oauthConfirm?oauth_token=abc123..."
}
Response Fields
OAuth 授权 URL:用户需要在浏览器中访问该 URL 并授予权限
OAuth Flow
调用 link endpoint
你的应用携带用户 ID 调用该 endpoint
重定向到授权 URL
将用户重定向到 link_web_url
处理 callback
Provider 重定向回 Mirobody Health,系统处理 callback 并保存凭据
Example Implementation
import aiohttp
async def link_provider(user_id: str, provider_slug: str, return_url: str = None):
params = {"user_id": user_id}
if return_url:
params["return_url"] = return_url
url = f"http://localhost:18080/api/v1/pulse/theta/{provider_slug}/link"
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params) as resp:
data = await resp.json()
return data["link_web_url"]
# Get authorization URL
auth_url = await link_provider("user_123", "theta_garmin")
# Redirect user to auth_url in browser
print(f"Please visit: {auth_url}")
Error Responses
{
"error": {
"code": "PROVIDER_NOT_FOUND",
"message": "Provider 'theta_unknown' not found or not supported"
}
}
{
"error": {
"code": "PROVIDER_NOT_CONFIGURED",
"message": "Provider 'theta_garmin' is not properly configured. Missing OAuth credentials."
}
}
授权 URL 有时效性(通常 15 分钟)。如果用户未在时限内完成授权,需要重新请求新的 link。
使用 return_url 参数可在链接成功后将用户带回你应用中的指定页面。