Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

send data to fitbit

I am writing code to retrieve users fitbit data into my application. Is there any way for my users to make entries into my application and for my application to send it to fitbit?

Best Answer
0 Votes
15 REPLIES 15
Best Answer
0 Votes
Where do you get the user id to put in the url? The documentation says use the "-" for the current logged in user. I tried that and that does not work.
Best Answer
0 Votes

How do you get the user id to put in the POST https://api.fitbit.com/1/user/[user-id]/body/log/weight.json ?

Best Answer
0 Votes

@jschrimshire wrote:
Where do you get the user id to put in the url? The documentation says use the "-" for the current logged in user. I tried that and that does not work.

It's not the logged in user, but rather the OAuth access token for the user that you're sending in the Authorization header. You can check what user a token belongs to by requesting the profile.

Best Answer
0 Votes

@jschrimshire wrote:

How do you get the user id to put in the POST https://api.fitbit.com/1/user/[user-id]/body/log/weight.json ?


The user id is returned with the access token at the end of the OAuth process.

Best Answer
0 Votes
Is there any example as to how you actually do a post? Please don't point me to the documentation. That is written from a 30,000 ft view.
If you can just give me a code snippet of say posting a users weight, I can do the rest. I have got it pulling in the users data for a given date. Do I set up the same kind of httpwebrequest and header as I did to retrieve the information? I have been working on this for 2 weeks now and feel like I am close.
Best Answer
0 Votes

Does anyone have a code snippet that shows how to post user data such as weight to the fitbit api? The documentation gives very little details as to how to do this. If there was just a simple example of posting one item, then I could figure out the rest. This is so frustrating. Thanks in advance.

Best Answer
0 Votes

@jschrimshire wrote:
Is there any example as to how you actually do a post? Please don't point me to the documentation. That is written from a 30,000 ft view.

POST is one of the HTTP methods, sometimes called HTTP's verbs. Most HTTP libraries allow you to specify the HTTP method easily. I'd point you to documentation, but...*

 

If you were using cURL, the request might look like:

curl \
-X POST \
-H "Authorization: Bearer reallylongoauth.token.here" \
--data "weight=65.7&date=2015-09-02&time=15:23:00" \
https://api.fitbit.com/1/user/-/body/log/weight.json

* If you do want to learn about HTTP methods, MDN has a good introduction.

Best Answer
0 Votes
Yes I did exactly that and it doesn't work. Not sure where to go from here.
Best Answer
0 Votes

Did you try the example with cURL? If so, what was the server's response?

Best Answer
0 Votes
I would rather do this without adding another open source library.
Can you please look at this code? I just can't figure out how to populate the data2 variable. I have tried every variation google has to offer. It would just be so nice if someone could post a code snippet similar to this that works.
This has been the most frustrating thing I have ever tried to do in my 20 years in IT.

string data2 = "weight=65.7&date=2015-08-13";
urlworkout = "https://api.fitbit.com/1/user/3MQRBF/body/log/weight/65.7/date/2015-08-13.json";
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(urlworkout);
request2.Method = WebRequestMethods.Http.Post;
request2.ContentLength = data2.Length;
request2.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request2.GetRequestStream());
writer.Write(data2);
writer.Close();
Best Answer
0 Votes

@jschrimshire wrote:

urlworkout = "https://api.fitbit.com/1/user/3MQRBF/body/log/weight/65.7/date/2015-08-13.json";


To create a weight log entry, the URL to POST to is https://api.fitbit.com/1/user/-/body/log/weight.json

 

Did you look at the server's response when you tried to use the URL in your example? It should have returned a HTTP 404 Not Found error, which lets you know when you're not using the correct URL.

Best Answer
0 Votes
No I do not get a 404 error or any error for that matter. It just does not log a weight.
I changed my url to the one in your answer and it still did not work.
If you do not place the actual weight into the url, then where do you put it?

I know that to log a weight, more is required than just having the correct url.
As I asked for in my last email, it would be so helpful if you or someone could just provide an example code snippet that would make this work.

All the documentation provides is post parameters and the url. This is not very helpful. I just need an example of the additional code required to make this work.
I have researched the httpwebrequest and have tried every code variation google provides but it still does not work. Please help.
Best Answer
0 Votes

@jschrimshire wrote:
As I asked for in my last email, it would be so helpful if you or someone could just provide an example code snippet that would make this work.

I provided a working cURL example earlier. I don't have a code example for the HTTP library that you're using.

 


@jschrimshire wrote:
No I do not get a 404 error or any error for that matter. It just does not log a weight.

The server returns some response. What is the response? Specifically, what is the HTTP status code and what is the body of the response say?

 


@jschrimshire wrote:
If you do not place the actual weight into the url, then where do you put it?

You put the information in the HTTP body as a form encoded values.

Best Answer
0 Votes
The code I am using is below. No error is returned. Please tell me what to do differently.

string data2 = "weight=65.7&date=2015-08-13";
urlworkout = "https://api.fitbit.com/1/user/3MQRBF/body/log/weight.json";
HttpWebRequest request2 = (HttpWebRequest)WebRequest.Create(urlworkout);
request2.Method = WebRequestMethods.Http.Post;
request2.ContentLength = data2.Length;
request2.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request2.GetRequestStream());
writer.Write(data2);
writer.Close();
Best Answer
0 Votes