joestump / python-oauth2

A fully tested, abstract interface to creating OAuth clients and servers.
MIT License
2.99k stars 912 forks source link

Error when requesting to_header: 'int' object has no attribute 'encode' #201

Closed rich-generator closed 9 years ago

rich-generator commented 9 years ago

I'm getting a strange error when trying to connect to Yahoo BOSS, which wasn't had no issues on a prior version of oauth2. I believe I'm currently using 1.9.0.post1 on Python 2.7.9.

Here's the code:

url = "http://yboss.yahooapis.com/ysearch/web?q=%s&count=%s&style=raw&abstract=long&market=en-gb&filter=-porn" % (search_query,noOfResults)
consumer = oauth2.Consumer(key=self.OAUTH_CONSUMER_KEY,secret=self.OAUTH_CONSUMER_SECRET)
                params = {
                    'oauth_version': '1.0',
                    'oauth_nonce': oauth2.generate_nonce(),
                    'oauth_timestamp': int(time.time()),
                }
oauth_request = oauth2.Request(method='GET', url=url, parameters=params)
                oauth_request.sign_request(oauth2.SignatureMethod_HMAC_SHA1(), consumer, None)
                oauth_header=oauth_request.to_header(realm="yahooapis.com")

This gives the following error:

  File ***, line 150, in search_yahoo
    oauth_header=oauth_request.to_header(realm="yahooapis.com")
  File "/usr/local/lib/python2.7/dist-packages/oauth2/__init__.py", line 398, in to_header
    params_header = ', '.join(header_params)
  File "/usr/local/lib/python2.7/dist-packages/oauth2/__init__.py", line 397, in <genexpr>
    header_params = ('%s="%s"' % (k, v) for k, v in stringy_params)
  File "/usr/local/lib/python2.7/dist-packages/oauth2/__init__.py", line 396, in <genexpr>
    stringy_params = ((k, escape(v)) for k, v in oauth_params)
  File "/usr/local/lib/python2.7/dist-packages/oauth2/__init__.py", line 163, in escape
    s = s.encode('utf-8')
AttributeError: 'int' object has no attribute 'encode'

It looks like it might be a bug, but I'm not sure whether it's just something has changed between versions and I'm now performing this wrong. Thoughts?

rich-generator commented 9 years ago

Actually, fixed it now. Turns out I had to convert the timestamp to a string, so from this:

params = {
                    'oauth_version': "1.0",
                    'oauth_nonce': oauth2.generate_nonce(),
                    'oauth_timestamp': int(time.time()),
                }

to this:

params = {
                    'oauth_version': "1.0",
                    'oauth_nonce': oauth2.generate_nonce(),
                    'oauth_timestamp': str(int(time.time()))
                }