2

I am trying to port the following curl call into a python script using urllib/urllib2 : curl -H "X-Api-Key: ccccccccccccccccccccccc" -H "X-Api-Secret: cbcwerwerwerwerwerwerweweewr9" https://api.assembla.com/v1/users/user.xml

I tried using the standard url call, but it failed:

url_assembla = 'https://api.assembla.com/v1/users/suer.xml'
base64string_assembla = base64.encodestring('%s:%s' %     ('ccccccccccccccccccccccc','cbcwerwerwerwerwerwerweweewr9')).replace('\n', '')

req_assembla = urllib2.Request(url_assembla)
req_assembla.add_header("Authorization", "Basic %s" % base64string_assembla) 
ET.parse(urllib2.urlopen(req_assembla))

Can any one advice on how to incorporate the Api-secret and Api-key. I want to do this as a script, so did not want to install the assembla package.

Thanks

tkansara
  • 534
  • 1
  • 4
  • 21

1 Answers1

1

If the curl command above works, you should perhaps just add the same headers into the urllib2 query?
Try this instead of the add_header("Authorization") line:

req_assembla.add_header("X-Api-Key", 'ccccccccccccccccccccccc')
req_assembla.add_header("X-Api-Secret", 'cbcwerwerwerwerwerwerweweewr9')
Igor Hatarist
  • 5,234
  • 2
  • 32
  • 45
  • Thanks. This worked. I did notice that the python script for the urlopen takes a bit longer than the Curl from cmd. Any ideas why ? – tkansara Feb 09 '15 at 17:59
  • @tkansara You're welcome! Sadly no, I have no idea. Perhaps parsing it with `xml.etree` takes some time if the `suer.xml` is pretty large? Or is it a `urlopen`'s fault? – Igor Hatarist Feb 09 '15 at 18:02
  • @tkansara there are some related questions on StackOverflow: 1) [Python urllib2.urlopen() is slow, need a better way to read several urls](http://stackoverflow.com/questions/3472515/python-urllib2-urlopen-is-slow-need-a-better-way-to-read-several-urls) 2) [How can I speed up fetching pages with urllib2 in python?](http://stackoverflow.com/questions/3490173/how-can-i-speed-up-fetching-pages-with-urllib2-in-python) (though the second one is more about threading) – Igor Hatarist Feb 09 '15 at 18:05
  • @tkansara I'd try the [requests](http://docs.python-requests.org/en/latest/) library. It's also easier to work with. – Igor Hatarist Feb 09 '15 at 18:06
  • @tkansara You're welcome :) If I helped you enough and my answers were OK, you can accept my answer by clicking the check mark near the answer's rating. – Igor Hatarist Feb 09 '15 at 18:27