instapaper and oauth - 403 "Not logged in" error

803 views Asked by At

I am trying to use the instapaper API, but I keep getting a 403 error for my requests. Here's the code:

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))

Any ideas?

2

There are 2 answers

0
pnsilva On BEST ANSWER

You're right about the signature method. But my main problem was that I wasn't handling the token appropriately. Here's the working code:

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)
0
jterrace On

First, make sure oauth2 is the library you're using. It's the most well-maintained python oauth module.

Second, this looks suspect:

client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1()

You're replacing the set_signature_method function. It should be:

client.set_signature_method(oauth.SignatureMethod_HMAC_SHA1())

You should follow the example here: https://github.com/simplegeo/python-oauth2/blob/master/example/client.py