2

我正在尝试使用 instapaper API,但我的请求不断收到 403 错误。这是代码:

consumer_key='...'
consumer_secret='...'
access_token_url = 'https://www.instapaper.com/api/1/oauth/access_token'

consumer = oauth.Consumer(consumer_key, consumer_secret)
client = oauth.Client(consumer)
client.add_credentials('...','...')

params = {}
params["x_auth_username"] = '..'
params["x_auth_password"] = '...'
params["x_auth_mode"] = 'client_auth'

client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1()
resp, token = client.request(access_token_url, method="POST",body=urllib.urlencode(params))
result = simplejson.load(urllib.urlopen('https://www.instapaper.com/api/1/bookmarks/list?' + token))

有任何想法吗?

4

2 回答 2

4

您对签名方法是正确的。但我的主要问题是我没有正确处理令牌。这是工作代码:

consumer = oauth.Consumer('key', 'secret')
client = oauth.Client(consumer)

# Get access token
resp, content = client.request('https://www.instapaper.com/api/1/oauth/access_token', "POST", urllib.urlencode({
    'x_auth_mode': 'client_auth',
    'x_auth_username': 'uname',
    'x_auth_password': 'pass'
}))

token = dict(urlparse.parse_qsl(content))
token = oauth.Token(token['oauth_token'], token['oauth_token_secret'])
http = oauth.Client(consumer, token)

# Get starred items
response, data = http.request('https://www.instapaper.com/api/1/bookmarks/list', method='POST', body=urllib.urlencode({
    'folder_id': 'starred',
    'limit': '100'
})) 
res = simplejson.loads(data)
于 2011-08-30T12:22:46.013 回答
3

首先,确保oauth2是您正在使用的库。它是维护得最好的 python oauth 模块。

其次,这看起来很可疑:

client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1()

您正在替换 set_signature_method 函数。它应该是:

client.set_signature_method(oauth.SignatureMethod_HMAC_SHA1())

你应该按照这里的例子:https ://github.com/simplegeo/python-oauth2/blob/master/example/client.py

于 2011-08-28T21:03:16.000 回答