2011-08-28 111 views
2

我正在嘗試使用instapaper API,但我一直在爲我的請求收到403錯誤。代碼如下:instapaper和oauth - 403「Not logged in」錯誤

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

任何想法?

+0

你需要你的訪問之前得到請求令牌令牌 – jterrace

+0

API文檔(http://www.instapaper.com/api/full)說,不需要請求令牌。 – pnsilva

+0

啊,我明白了。 ''client.request''返回的''token'是否有效?你應該直接使用OauthRequest而不是urllib.urlopen。請參閱https://github.com/simplegeo/python-oauth2/blob/master/example/client.py#L145-156 – jterrace

回答

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)