Spot any errors? let me know, but Unleash your pedant politely please.

…

Wednesday, 27 February 2013

Requests revisited.

Further to not so stupid after all, the pain of oauth just keeps giving.  I basically got a bunch of stuff working with Requests that was previously working with the old libraries, but in a simpler way.  On that foundation, I tried to once again upload a file to Project Place.

No joy. The request was badly signed. Same problem as before.

I Googled. I searched the Requests issues log etc, but there wasn't much information.

So I contacted the developer at Project Place to explain my problem.  The initial response was the same as before, that I only need to sign the headers, not the body of the HTTP request.   The problem remained that I wasn't in control of that stuff. I was using a library to do it all for me.  I could get an Oauth object by supplying some parameters to something I didn't fully understand and I could send a (GET) request by a url and supplying a nice dictionary of parameters to something else I didn't fully understand.

Jon at Project Place then very kindly sent me some client code that used the Request library to upload a file successfully to Project Place. Now that I had some working code in front of me, I could try to figure it out and then try to figure out where Requests was going wrong. I could see how Jon's code signed the request headers before adding the body - there's no way to specify this in Requests.  I found the Requests code that does the signing. Now one of the issues made a bit more sense. I took a local copy and did a little tweaking, and things seemed to be much better.

While it works, it's not really the correct fix. The problem is (apparently) in requests-oauthlib, but I don't have the knowledge required to fix it.

I didn't think a local copy of Requests was really a long term solution, so today I started to refactor in order to use Jon's code.  It took a while to get anything working at all due Jon's code's handling of parameters: it doesn't (at least not for GET or PUT).  Fortunately he'd commented really well, and once I'd RTFMed, I could see I needed to first add them to the URL - the kind of inconvenience I was hoping to avoid by using Requests.

WAY too much effort went into this line:

url = unicode('%s?%s'%(request,urlencode(params)))

It appears to be working now, with a standard Requests library, plus Jon's code.  It's a just a lot more untidy than I'd hoped for :(

No comments:

Post a Comment