python - Requesting Token via Django Piston Throws TypeError Exception -


when trying authenticate via oauth in django piston, following exception thrown:

environment:  request method: request url: http://localhost:8000/api/oauth/request_token/?oauth_nonce=32921052&oauth_timestamp=1291331173&oauth_consumer_key=ghof7av2vu8hal2hek&oauth_signature_method=hmac-sha1&oauth_version=1.0&oauth_signature= python version:    traceback: file "/users/derek/.virtualenvs/optimal-rest/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response   100.                     response = callback(request, *callback_args, **callback_kwargs) file "/users/derek/.virtualenvs/optimal-rest/lib/python2.7/site-packages/piston/authentication.py" in oauth_request_token   130.         token = oauth_server.fetch_request_token(oauth_request) file "/users/derek/.virtualenvs/optimal-rest/lib/python2.7/site-packages/piston/oauth.py" in fetch_request_token   302.             self._check_signature(oauth_request, consumer, none) file "/users/derek/.virtualenvs/optimal-rest/lib/python2.7/site-packages/piston/oauth.py" in _check_signature   393.         valid_sig = signature_method.check_signature(oauth_request, consumer, token, signature) file "/users/derek/.virtualenvs/optimal-rest/lib/python2.7/site-packages/piston/oauth.py" in check_signature   482.         built = self.build_signature(oauth_request, consumer, token) file "/users/derek/.virtualenvs/optimal-rest/lib/python2.7/site-packages/piston/oauth.py" in build_signature   513.             hashed = hmac.new(key, raw, sha) file "/usr/local/cellar/python/2.7/lib/python2.7/hmac.py" in new   133.     return hmac(key, msg, digestmod) file "/usr/local/cellar/python/2.7/lib/python2.7/hmac.py" in __init__   72.         self.outer.update(key.translate(trans_5c))  exception type: typeerror @ /api/oauth/request_token/?oauth_nonce=32921052&oauth_timestamp=1291331173&oauth_consumer_key=ghof7av2vu8hal2hek&oauth_signature_method=hmac-sha1&oauth_version=1.0&oauth_signature= exception value: character mapping must return integer, none or unicode 

can't tell if it's bug in piston or if can't use oauth2 lib.

consumer code:

import os import cgi import oauth2 oauth  # settings local test consumer consumer_server = os.environ.get("consumer_server") or 'localhost' consumer_port = os.environ.get("consumer_port") or '8000' print consumer_server , consumer_port   # fake urls test server (matches ones in server.py) request_token_url = 'http://%s:%s/api/oauth/request_token/' % (consumer_server, consumer_port) access_token_url = 'http://%s:%s/api/oauth/access_token/' % (consumer_server, consumer_port) authorize_url = 'http://%s:%s/api/oauth/authorize/' % (consumer_server, consumer_port)  # key , secret granted service provider consumer application - same mockoauthdatastore consumer_key = 'ghof7av2vu8hal2hek' consumer_secret = 'ohhey'  consumer = oauth.consumer(consumer_key, consumer_secret) client = oauth.client(consumer)  # step 1: request token. temporary token used  # having user authorize access token , sign request obtain  # said access token.  resp, content = client.request(request_token_url, "get") if resp['status'] != '200':     raise exception("invalid response %s." % resp['status']) 

referencing https://github.com/clemesha/django-piston-oauth-example consumer code.

this piston problem comes encoding problem of key/secret of consumer. solution force encoding of key/secret returned database ascii.

in store.py file of piston, modify lookup_consumer this:

def lookup_consumer(self, key):     try:         self.consumer = consumer.objects.get(key=key)         self.consumer.key = self.consumer.key.encode('ascii')         self.consumer.secret = self.consumer.secret.encode('ascii')         return self.consumer     except consumer.doesnotexist:         return none 

here fork of django-piston fixing problem.


Comments