instapaper和oauth - 403“未登录”错误

时间:2011-08-28 16:09:04

标签: python oauth instapaper

我正在尝试使用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))

有什么想法吗?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分: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