我正在try 从MyFitnessPal获取我的日记,本机我编写了以下有效的代码:
"use strict";
const axios = require('axios')
async function getReport(username) {
const foodLog = await axios({
method: 'POST',
url: `https://www.myfitnesspal.com/api/services/authenticate_diary_key/${username}`,
data: {
from: "2023-01-20",
to: "2023-01-30",
show_food_diary: 1,
show_exercise_diary: 1,
show_food_notes: 0,
show_exercise_notes: 0,
key: ""
}
})
// do stuff with foodLog
}
响应包含一组日期,每个日期包含膳食等.
然而,当我向MyFitnessPal发出完全相同的请求时,但在使用UrlFetchApp的Google应用程序脚本中,我得到了一个空响应:
function getReport(username) {
const url = `https://www.myfitnesspal.com/api/services/authenticate_diary_key/${username}`
const payload = {
from: "2023-01-20",
to: "2023-01-30",
show_food_diary: 1,
show_exercise_diary: 1,
show_food_notes: 0,
show_exercise_notes: 0,
key: ""
}
const options = {
payload,
method: 'post'
}
try {
const response = UrlFetchApp.fetch(url,options)
console.log(response.getResponseCode()) //200
console.log(response.getContentText()) // []
} catch (error){
console.log(error)
}
}
请求通过时没有出现状态为200的错误,但我得到了一个emty数组作为响应.我是不是遗漏了什么?
通过将MyFitnessPal用户名传递给getReport(),您可以很容易地复制该错误,但请确保日记共享设置为"Public",并且您已经记录了所选日期的膳食.