how to convert this curl command to some Python codes that do the same thing?
Question: I am trying to download my data by using Fitbit API. I have figured out how to
obtain a certain day's data, which is good. And here is the curl command I
used:

    
    
    curl -i -H "Authorization: Bearer (here goes a very long token)" https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json >> heart_rate_20160614.json
    

However, I would like to collect hundreds of days' data and I don't want to do
that manually. So I think I could use a Python loop. I read some other topics
like [this one](http://stackoverflow.com/questions/3246021/python-equivalent-
of-curl-http-post) and [this
one](http://stackoverflow.com/questions/31507988/trying-to-convert-a-curl-
post-with-authorization-to-python) but still don't know how to 'translate'
these curl commands into python language by using urllib2.

I have tried this:

    
    
    import urllib2
    url = 'https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/1sec/time/00:00/00:01.json'
    data = '{Authorization: Bearer (here goes a very long token)}'
    req = urllib2.Request(url,data)
    f = urllib2.urlopen(req)
    

but the got an error says "HTTP Error 404: Not Found"

So what is the correct way to 'translate' this curl command to python
language? Thanks!


Answer: The problem comes from the construction of the `Request` object : by default,
the second parameter is the data that you want to pass along with the request.
Instead, you have to specify that you want to pass headers. This is the
correct way to do it :

    
    
    import urllib2
    url = 'https://api.fitbit.com/1/user/-/activities/heart/date/2016-6-14/1d/1sec/time/00:00/23:59.json'
    hdr = {'Authorization': 'Bearer (token)'}
    req = urllib2.Request(url,headers=hdr)
    f = urllib2.urlopen(req)
    

This wields a 401 on my side, but should work with your token.

You can have more informations on urllib2 (and the Request class)
[here](https://docs.python.org/2/library/urllib2.html#urllib2.Request)

However, I suggest you take a look at [Requests](http://docs.python-
requests.org/en/master/), which is in my opinion easier to use, and very well
documented.

Hope it'll be helpful.

