Nothing clever on my part at all, other then patience, the work of others and a tiny epiphany. Where by 'epiphany', I mean to say I used my noodle briefly.
Having noticed that Requests was up to 1.1.0, I installed this new version…
$ sudo pip install requests --upgrade
This didn't work. Fortunately though, I'd logged the errors I was getting in the previous posts. The error has regressed. I took a look at the documentation for Oauth, which reminded me that the Oauth gubbins that requests uses had been split out to requests-oauthlib.
I done another pip [sic]…
$ sudo pip install requests-oauthlib --upgrade
This time I got ValueError, which was new…
Traceback (most recent call last): File "/Users/bob/Dropbox/Coding/Python/ProjectPlace (requests)/requests_test.py", line 26, inr = requests.get(u'https://api.projectplace.com/1/user/me/profile.json', auth=auth) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 55, in get return request('get', url, **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/api.py", line 44, in request return session.request(method=method, url=url, **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/sessions.py", line 276, in request prep = req.prepare() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 227, in prepare p.prepare_auth(self.auth) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/models.py", line 400, in prepare_auth r = auth(self) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests_oauthlib/core.py", line 53, in __call__ unicode(r.url), unicode(r.method), None, r.headers) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 228, in sign raise ValueError('Body signatures may only be used with form-urlencoded content') ValueError: Body signatures may only be used with form-urlencoded content
Had I been paying attention, there was a pretty big clue at the end…
ValueError: Body signatures may only be used with form-urlencoded content
I wasn't paying attention though, so I added an exception handler to ensure that I would run through all signature types. and to get a bit more information. This wasn't a complete waste of time…
Running this code…
import requests
from requests_oauthlib import OAuth1
#Hywel:
consumer_key = "That"
consumer_secret = "would"
access_token_key = "be"
access_token_secret = "telling"
for signature_type in ('auth_header','body','query'):
auth = OAuth1(client_key = consumer_key,
client_secret = consumer_secret,
resource_owner_key = access_token_key,
resource_owner_secret = access_token_secret,
signature_type = signature_type)
try:
r = requests.get(u'https://api.projectplace.com/1/user/me/profile.json', auth=auth)
except ValueError,e:
print 'Exception for %s:%s'%(signature_type,e)
print
else:
print 'No Exception for %s'%signature_type
print signature_type
print r.encoding
print r.text
print r.json
Gave me these very useful results…
No Exception for auth_header
auth_header
None
{"province": "", "city": "", "first_name": "Hywel", "last_name": "Thomas", "address2": "", "home_phone": "", "language": "english", "title": "Software Tester", "work_phone": "", "address1": "", "description": "", "id": that, "email": "would", "mobile_phone": "+be", "avatar": "/images/00/1f/34/da6c-a6d5a995.jpeg", "country_code": "GB", "organisation_homepage": "", "sort_name": "Thomas, Hywel", "homepage": "", "organisation_name": "Telling", "zip_code": ""}
[bound method Response.json of [Response [200]]
Exception for body:Body signatures may only be used with form-urlencoded content
body
None
{"province": "", "city": "", "first_name": "Hywel", "last_name": "Thomas", "address2": "", "home_phone": "", "language": "english", "title": "Software Tester", "work_phone": "", "address1": "", "description": "", "id": that, "email": "would", "mobile_phone": "+be", "avatar": "/images/00/1f/34/da6c-a6d5a995.jpeg", "country_code": "GB", "organisation_homepage": "", "sort_name": "Thomas, Hywel", "homepage": "", "organisation_name": "Telling", "zip_code": ""}
[bound method Response.json of [Response [200]]
No Exception for query
query
None
{"province": "", "city": "", "first_name": "Hywel", "last_name": "Thomas", "address2": "", "home_phone": "", "language": "english", "title": "Software Tester", "work_phone": "", "address1": "", "description": "", "id": that, "email": "would", "mobile_phone": "+be", "avatar": "/images/00/1f/34/da6c-a6d5a995.jpeg", "country_code": "GB", "organisation_homepage": "", "sort_name": "Thomas, Hywel", "homepage": "", "organisation_name": "Telling", "zip_code": ""}
[bound method Response.json of [Response [200]]
I don't fully understand what Body signatures may only be used with form-urlencoded content means at the moment, but I kind of don't care. I think I just need to know that with the latest versions of requests and requests-oauthlib installed and a signature_type of 'auth_header' or 'query', I should be good to go.
No comments:
Post a Comment