4

这是我的 Oauth 代码:

auth = OAuth1(client_key=consumer_key,
              client_secret=consumer_secret,
              resource_owner_key=access_key,
              resource_owner_secret=access_secret,
              signature_type='auth_header')

r = requests.get(url, auth=auth)
print r

错误:

   r = requests.get(url,auth = auth)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 49, in get
    return request('get', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 38, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 259, in request
    prep = req.prepare()
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 197, in prepare
    p.prepare_auth(self.auth)
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 329, in prepare_auth
    r = auth(self)
  File "/usr/local/lib/python2.7/dist-packages/requests_oauthlib/core.py", line 41, in __call__
    decoded_body = extract_params(r.data)
AttributeError: 'PreparedRequest' object has no attribute 'data'
4

2 回答 2

2

我猜你正在使用 Requests 1.0.0 或更高版本。这是一个向后不兼容的版本,发生此错误是因为requests-oauthlib尚不兼容。已经做了一些工作来解决这个问题,但还没有一个新版本。请参阅requests-oauthlib 的问题 #1

现在你的选择是:

  1. 降级到 requests 0.14.2,它应该与 request_oauthlib 0.2.0 一起使用。
  2. 升级到request_oauthlib的 git 版本,它可能足以满足您的需求。
于 2012-12-26T16:01:44.040 回答
1

如果您愿意尝试包装 Requests 的库,那么可以使用rauth。我什至可以给你一个有效的 Tumblr 示例:

from rauth import OAuth1Service

import re
import webbrowser

# Get a real consumer key & secret from http://www.tumblr.com/oauth/apps
tumblr = OAuth1Service(
    consumer_key='gKRR414Bc2teq0ukznfGVUmb41EN3o0Nu6jctJ3dYx16jiiCsb',
    consumer_secret='DcKJMlhbCHM8iBDmHudA9uzyJWIFaSTbDFd7rOoDXjSIKgMYcE',
    name='tumblr',
    request_token_url='http://www.tumblr.com/oauth/request_token',
    access_token_url='http://www.tumblr.com/oauth/access_token',
    authorize_url='http://www.tumblr.com/oauth/authorize',
    base_url='https://api.tumblr.com/v2/')

request_token, request_token_secret = tumblr.get_request_token()

authorize_url = tumblr.get_authorize_url(request_token)

print 'Visit this URL in your browser: ' + authorize_url
webbrowser.open(authorize_url)
authed_url = raw_input('Copy URL from your browser\'s address bar: ')
verifier = re.search('\oauth_verifier=([^#]*)', authed_url).group(1)

session = tumblr.get_auth_session(request_token,
                                  request_token_secret,
                                  method='POST',
                                  data={'oauth_verifier': verifier})

user = session.get('user/info').json()['response']['user']

print 'Currently logged in as: {name}'.format(name=user['name'])

Rauth 0.5.3 将适用于 Requests 1.1.0(可能还有 1.0.0)。如果您试一试,如果您还有其他问题,请告诉我!

完全披露,我保持沉默。

于 2013-03-29T03:32:08.070 回答