Response unpacking error in Python 2.7 with python-oauth2 and Imgur's Authenticated API
Salutations!
I've been working on a multiple-service file uploader that filters file extensions to decide which service a file should be uploaded to. My primary target is uploading to an Imgur user account, as its API is the most complicated in contrast to other services (i.e. authentication with OAuth2). I previously had issues with connecting via SSL, as my HTTPLib certificate store would not validate an SSL Handshake, but I fixed that by manually adding Imgur's cert provider's CA to the cert list.
Anyways, I've been able to "log in" to Imgur using cookies, but I'd much rather be using oauth - the cookie method still uses the anonymous API, which has a lower upload limit. I'm attempting this by generating an Oauth authentiction URL, and using wxPython to make a simple text entry dialog asking for the PIN given by said URL when the user provides their credentials. The problem is that calling the authenticator for python-oauth2 results in a gzip unpacking error, which I have no idea how to combat. Here's the error:
Traceback (most recent call last):
File "C:\Users\Austin\Programming\python\uploaderator\mk2\main.py", line 10, in <module>
uploader = crumpet.Crumpet()
File "C:\Users\Austin\Programming\python\uploaderator\mk2\crumpet.py", line 30, in __init__
s.connect()
File "C:\Users\Austin\Programming\python\uploaderator\mk2\imgurHandler.py", line 40, in connect
self.authorize(pin)
File "C:\Users\Austin\Programming\python\uploaderator\mk2\oauthHandler.py", line 28, in authorize
resp, content = client.request(self.access_token_url, "POST")
File "build\bdist.win32\egg\oauth2\__init__.py", line 682, in request
File "C:\Python27\lib\site-packages\httplib2-0.7.1-py2.7.egg\httplib2\__init__.py", line 1436, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "C:\Python27\lib\site-packages\httplib2-0.7.1-py2.7.egg\httplib2\__init__.py", line 1188, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "C:\Python27\lib\site-packages\httplib2-0.7.1-py2.7.egg\httplib2\__init__.py", line 1174, in _conn_开发者_运维百科request
content = _decompressContent(response, content)
File "C:\Python27\lib\site-packages\httplib2-0.7.1-py2.7.egg\httplib2\__init__.py", line 384, in _decompressContent
content = gzip.GzipFile(fileobj=StringIO.StringIO(new_content)).read()
File "C:\Python27\lib\gzip.py", line 245, in read
self._read(readsize)
File "C:\Python27\lib\gzip.py", line 316, in _read
self._read_eof()
File "C:\Python27\lib\gzip.py", line 334, in _read_eof
crc32 = read32(self.fileobj)
File "C:\Python27\lib\gzip.py", line 25, in read32
return struct.unpack("<I", input.read(4))[0]
struct.error: unpack requires a string argument of length 4
My authorization function, which is called after a pin is acquired, is this:
def authorize(self, pin):
self.token_u.set_verifier(pin)
client = oauth.Client(self.consumer, self.token_u)
resp, content = client.request(self.access_token_url, "POST")
access_token = dict(urlparse.parse_qsl(content))
self.oauth_token = access_token['oauth_token']
self.oauth_token_secret = access_token['oauth_token_secret']
self.token = oauth.Token(self.oauth_token, self.oauth_token_secret)
self.client = oauth.Client(self.consumer, self.token)
if resp['status'] == '200':
return True
else:
return False
self.access_token_url is https://api.imgur.com/oauth/access_token, as given by the Imgur API Authentication resources.
I'm not sure if this is a problem with my code, or with what Imgur is returning with their response, as there are other python-based uploaders that appear to work fine, using very similar methods. Like I mentioned in the title, I'm using Python 2.7 and python-oauth2.
I would greatly appreciate any input. Thank you for your time.
Protractor Ninja
I'm using a requests-oauth for python, and I think the issue I was having, was my parser was returning the tokens as lists of one item long. So what I did was just get tokenList[0]
, which solved my issue.
Hope it helps, over a year later.
精彩评论