【问题标题】:Google fit aggregate rest api call issue parse error javascriptGoogle fit 聚合 rest api 调用问题解析错误 javascript
【发布时间】:2019-06-28 02:04:33
【问题描述】:

我正在调用 google fit rest api 如下

 const requestBody ={
    "aggregateBy": [{
      "dataTypeName": "com.google.step_count.delta",
      "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
    }],
    "bucketByTime": { "durationMillis": 86400000 },
    "startTimeMillis": 1561228200000,
    "endTimeMillis": 1561652514300
    }

const userAction = async () => {
    const response = await fetch('https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate', {
      method: 'POST',
      body: requestBody, 
      headers: {
      'Content-Type': 'application/json',
      'Content-Length': '302',
      'Authorization': 'Bearer ' + authcode,
      }
    });
    const jsonResponse = await response.json();
    console.log(jsonResponse);
  }
  userAction();

我得到了回应

{ "error": { "errors": [{ "domain": "global", "reason": "parseError", "message": "Parse Error" }], "code": 400, "message": "Parse Error" } }

不确定解析错误发生在哪里。任何指向它发生位置的帮助将不胜感激。 注意 - 授权令牌被正确使用,所以它可能不是问题。 我也在本地主机上运行。

【问题讨论】:

    标签: javascript google-api google-fit google-fit-sdk


    【解决方案1】:

    通过以下代码让它工作(我有硬编码的静态参数)。添加 api 密钥和客户端 ID 并运行它就可以了。 此链接有帮助 - https://developers.google.com/fit/rest/v1/reference/users/dataset/aggregate

    <script>
    
      function authenticate() {
        return gapi.auth2.getAuthInstance()
            .signIn({scope: "https://www.googleapis.com/auth/fitness.activity.read"})
            .then(function() { console.log("Sign-in successful"); },
                  function(err) { console.error("Error signing in", err); });
      }
      function loadClient() {
        gapi.client.setApiKey("YOUR APP ID");
        return gapi.client.load("https://content.googleapis.com/discovery/v1/apis/fitness/v1/rest")
            .then(function() { console.log("GAPI client loaded for API"); },
                  function(err) { console.error("Error loading GAPI client for API", err); });
      }
      // Make sure the client is loaded and sign-in is complete before calling this method.
      function execute() {
        return gapi.client.fitness.users.dataset.aggregate({
          "userId": "me",
          "resource": {
            "aggregateBy": [
              {
                "dataTypeName": "com.google.step_count.delta",
                "dataSourceId": "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
              }
            ],
            "endTimeMillis": 1561652514300,// Any time in milliseconds
            "startTimeMillis": 1561228200000,// Any time in milliseconds
            "bucketByTime": {
              "durationMillis": 86400000// Any duration in milliseconds
            }
          }
        })
            .then(function(response) {
                    // Handle the results here (response.result has the parsed body).
                    console.log("Response", response);
                  },
                  function(err) { console.error("Execute error", err); });
      }
      gapi.load("client:auth2", function() {
        gapi.auth2.init({client_id: "YOUR CLIENT ID"});
      });
    </script>
    <button onclick="authenticate().then(loadClient)">authorize and load</button>
    <button onclick="execute()">execute</button>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-14
      • 2023-04-06
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      • 2018-05-01
      • 1970-01-01
      相关资源
      最近更新 更多