如何解决Google Fit Api 暂时不稳定吗?
我开发了一个 MEAN(Mongodb、Express、Angular 和 Node.js)堆栈网络应用程序,用于从 Google Fit 帐户访问这些步骤。但有时它会返回步骤 (inVal),有时它不会返回 Point 数组中没有值的值。有一段时间它无法获取用户步骤并且 Oauth didin.t 再次重定向到主页。我可以知道为什么吗?这是创建 api 的片段。
googleRoute.route('/getURLTing/:id').get((req,res,err) => {
userid = req.params.id;
const oauth2client = new google.auth.OAuth2(
// client id
"42049410831-xxxxxxxx.apps.googleusercontent.com",// client secret
"yjQutKglr3xxxxxxxxxxV1VL",// link to redirect to
"http://localhost:1234/api/steps"
);
const scopes = [ "https://www.googleapis.com/auth/fitness.activity.read profile email openid"];
const url = oauth2client.generateAuthUrl({
access_type: "offline",scope: scopes,state: JSON.stringify({
callbackUrl: req.body.callbackUrl,userID: req.body.userid
})
});
request(url,(err,body => {
// console.log("error: ",err);
console.log("statusCode: ",res && res.statusCode);
// res.send({ url});
// console.log('URL= ',url);
console.log("redirect to google auth");
// var URL = url.toString();
// res.status(301).redirect(URL);
return res.json(url);
}));
});
googleRoute.route('/steps').get(async (req,res) => {
const queryURL = new urlParse(req.url);
const acode = queryParse.parse(queryURL.query).code;
const oauth2client = new google.auth.OAuth2(
// client id
"42049410831-xxxxxxxx.apps.googleusercontent.com",// link to redirect to
"http://localhost:1234/api/steps"
// "http://localhost:4200/home"
);
const tokens = await oauth2client.getToken(acode);
var m = getMillis();
var l = getMillisAtMidnight();
var initDate = getinitialTImeDateMidnight();
console.log('initDate = ',initDate);
console.log('Time in milliseconds = ',m);
let stepArray = [];
try{
const result = await axios ({
method: "POST",headers: {
authorization: "Bearer " + tokens.tokens.access_token
},"Content-Type": "application/json",url: `https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate`,data: {
"aggregateBy" : [{
dataTypeName: "com.google.step_count.delta",dataSourceId: "derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
}],"bucketByTime": { "durationMillis": x },// This is 24 hours
//"startTimeMillis": 1617033600000,// Mock start time
"startTimeMillis": initDate,// Real start time
"endTimeMillis": initDate + x // End Time
}
});
stepArray = result.data.bucket;
for(const dataSet of stepArray){
for(const points of dataSet.dataset){
for(const steps of points.point){
console.log('Number of steps = ',steps.value[0].intVal);
var s = steps.value[0].intVal;
User.findById(userid,function(err,user) {
if (user) {
var temp = user.steps;
let data = {
step: s,date: m,}
temp.push(data)
let data_steps = {
steps: temp
}
User.updateOne({ _id: user._id },data_steps)
.then(result => {
res.redirect('http://localhost:4200/home');
})
.catch(err => {
res.status(404).json({ error: "ERROR: Failed to save data steps" })
})
} else {
return res.status(404).json({ error: "ERROR: Failed to find user" });
}
});
// })
}
}
}
} catch (e) {
console.log(e);
}
});
我也在 Google Developer 工作,但仍然没有返回任何值。相信我,一次就好了。然后它碰巧没有成功。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。