04-29-2016 23:36
04-29-2016 23:36
I try use POST https://api.fitbit.com/1/user/[user-id]/foods/log.json
and I get {"errors":[{"errorType":"validation","fieldName":"date","message":"Date is required"}]}
I am sure the token work and successful set header for the request. Here is the code
// This part set the post parameters
JSONObject jsonObj = new JSONObject();
jsonObj.put("foodId", null);
jsonObj.put("foodName", name.getText().toString());
jsonObj.put("mealTypeId", 1);
jsonObj.put("unitId", 304);
jsonObj.put("amount", amount.getText().toString());
jsonObj.put("date", "2016-04-29");
jsonObj.put("favorite", false);
jsonObj.put("brandName", null);
jsonObj.put("calories", null);
new UploadLog().execute(jsonObj.toString());//This part set the header and set HTTP post
URL url = new URL(String.format(fitbit.loggingFoodRequest, fitbit.userID));
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + fitbit.token);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
//This part writes the parameters into the post request
Uri.Builder builder = new Uri.Builder().appendQueryParameter("json", params[0]);
String query = builder.build().getEncodedQuery();
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
Any one can help me, Thanks
04-30-2016 04:26
04-30-2016 04:26
I already solve this problem, but any one try to use foodId to upload a food log.
Best Answer01-30-2017 20:52
01-30-2017 20:52
Hi
Can you please tell how you solve this issue because i am facing same (Date required ) issue.
Thanks
Sushil jindal
Best Answer02-01-2017 10:58
Community Moderator Alumni are previous members of the Moderation Team, which ensures conversations are friendly, factual, and on-topic. Moderators are here to answer questions, escalate bugs, and make sure your voice is heard by the larger Fitbit team. Learn more
02-01-2017 10:58
@Doug_MSExplorer Can I see the request you're making?
Best Answer06-15-2017 09:53
06-15-2017 09:53
I know this thread is old, but a Google search for " FitBit 'Date is required' " has only one applicable result (this one). I ran into this problem yesterday, since I just started using FitBit. Coming from a native developer background, using web APIs in code is new to me. What I found out is that you do not POST a JSON body with all the data you want to store. All of your data is posted with a query string.
If you are creating a JSON body like this...
{
"foodLog":{
"isFavorite":true,
"logDate":"2011-06-29",
"logId":21633,
"loggedFood":{
"accessLevel":"PUBLIC",
"amount":2.55,
"brand":"",
"calories":370,
"foodId":82294,
"mealTypeId":7,
"locale":"en_US",
"name":"Chips",
"unit":{"id":304,"name":"serving","plural":"servings"},
"units":[304,226,180,147,389]
},
"nutritionalValues":{
"calories":370,
"carbs":47,
"fat":17.5,
"fiber":5,
"protein":5,
"sodium":325
}
}
}...and post to
https://api.fitbit.com/1/user/-/foods/log.json
you will get the "Date is Required" error. Instead, you build the POST string like this:
https://api.fitbit.com/1/user/-/foods/log.json?date="2017-6-15"&_______
where _____ is the rest of your food log data. I hope this helps anyone else who comes across this post.
Best Answer