gpt4 book ai didi

javascript - Google fit aggregate rest api调用问题解析错误javascript

转载 作者:行者123 更新时间:2023-11-29 22:54:47 24 4
gpt4 key购买 nike

我调用 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();

我得到的回应是

{
“错误”: {
“错误”:[{
“域”:“全局”,
“原因”:“解析错误”,
"message": "解析错误"
}],
“代码”:400,
"message": "解析错误"
}
}

不确定解析错误发生在何处。任何指向它发生的地方的帮助将不胜感激。注意 - 正确获取了身份验证 token ,因此它可能不是问题所在。我也在本地主机上运行。

最佳答案

通过以下代码让它工作(我有硬编码的静态参数)。添加 api key 和客户端 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>

关于javascript - Google fit aggregate rest api调用问题解析错误javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56800140/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com