0

问候!

我一直在研究一个多服务文件上传器,它过滤文件扩展名以决定文件应该上传到哪个服务。我的主要目标是上传到 Imgur 用户帐户,因为它的 API 与其他服务相比是最复杂的(即使用 OAuth2 进行身份验证)。我以前在通过 SSL 连接时遇到问题,因为我的 HTTPLib 证书存储不会验证 SSL 握手,但我通过手动将 Imgur 的证书提供者的 CA 添加到证书列表来解决这个问题。

无论如何,我已经能够使用 cookie “登录”到 Imgur,但我更愿意使用 oauth - cookie 方法仍然使用匿名 API,它具有较低的上传限制。我正在尝试通过生成一个 Oauth 身份验证 URL,并使用 wxPython 创建一个简单的文本输入对话框,在用户提供凭据时询问所述 URL 给出的 PIN。问题是调用 python-oauth2 的身份验证器会导致 gzip 解包错误,我不知道如何解决。这是错误:

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

我的授权函数,在获取 pin 后调用,是这样的:

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 是https://api.imgur.com/oauth/access_token,由 Imgur API 身份验证资源给出。

我不确定这是否是我的代码有问题,或者 Imgur 返回的响应是否有问题,因为还有其他基于 python 的上传器使用非常相似的方法似乎可以正常工作。就像我在标题中提到的那样,我使用的是 Python 2.7 和python-oauth2

我将不胜感激任何意见。感谢您的时间。

量角器忍者

4

1 回答 1

0

我正在为 python 使用 requests-oauth,我认为我遇到的问题是我的解析器将令牌作为一个项目的列表返回。所以我所做的只是得到tokenList[0],这解决了我的问题。

希望一年多以后有帮助。

于 2012-04-20T17:37:06.440 回答