You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ReactJS调用Google Fit API无法获取步数数据问题求助

问题排查:Google Fit API返回空步数points数组

以下是几个可能导致你获取不到步数数据的原因及解决办法:

  • DataSourceId拼写错误
    你当前使用的dataSourceId末尾多了ltas后缀,标准的步数衍生数据源ID应为:

    derived:com.google.step_count.delta:com.google.android.gms:estimated_steps
    

    错误的ID会指向不存在的数据源,自然返回空的point数组。修改请求中的dataSourceId字段即可。

  • 权限未正确配置
    确认你的OAuth 2.0授权流程已请求https://www.googleapis.com/auth/fitness.activity.read权限。如果缺少该权限,API不会报错,但会返回空数据,这是容易忽略的细节。检查你的授权范围是否包含该权限。

  • DatasetId时间范围不够精准
    虽然你将毫秒转纳秒的逻辑是对的,但起始时间100000过于宽泛,建议直接设置为上周的起始时间(转成纳秒),精准定位到有数据的时间段:

    const now = Date.now();
    const lastWeekStart = now - 7 * 24 * 60 * 60 * 1000; // 上周此刻的时间戳(毫秒)
    const datasetId = `${lastWeekStart}000000-${now}000000`;
    
  • 步数数据存储在其他数据源
    如果上述调整后仍无数据,说明你的步数可能存在非衍生的原生数据源中。先调用users.dataSources.list接口获取所有步数相关数据源,再逐个查询:

    const listAllStepSources = async () => {
      const now = Date.now();
      const lastWeekStart = now - 7 * 24 * 60 * 60 * 1000;
      const sourcesRes = await gapi.client.fitness.users.dataSources.list({
        userId: "me",
        dataTypeName: "com.google.step_count.delta"
      });
    
      for (const source of sourcesRes.data.dataSources) {
        const stepRes = await gapi.client.fitness.users.dataSources.datasets.get({
          userId: "me",
          dataSourceId: source.dataStreamId,
          datasetId: `${lastWeekStart}000000-${now}000000`
        });
        console.log(`数据源${source.dataStreamId}的步数:`, stepRes.data.point);
      }
    };
    

内容的提问来源于stack exchange,提问作者Little_bugfixer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 20:38:10