使用 python-oauth2 和 Imgur 的经过身份验证的 API 在 Python 2.7 中响应解包错误

发布于 2024-11-29 13:45:53 字数 3312 浏览 0 评论 0原文

致敬!

我一直在开发一个多服务文件上传器,它可以过滤文件扩展名来决定文件应该上传到哪个服务。我的主要目标是上传到 Imgur 用户帐户,因为与其他服务(即使用 OAuth2 进行身份验证)相比,它的 API 是最复杂的。我以前在通过 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

我将非常感谢任何意见。感谢您抽出时间。

量角器忍者

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自找没趣 2024-12-06 13:45:53

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

希望一年多后能有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文