2013-02-10 18 views
8

我正在嘗試使用Instapaper's simple developer api來使用python和請求庫向我的書籤添加一個url。驗證用戶名和密碼都可以正常工作。Python使用三個參數請求庫HTTPBasicAuth

import requests 
from requests.auth import HTTPBasicAuth 
requests.get('https://www.instapaper.com/api/authenticate', auth=HTTPBasicAuth('username', 'password')) 

但是,試圖使用API​​添加書籤時:

requests.get('https://www.instapaper.com/api/add', auth=HTTPBasicAuth('username', 'password','websiteUrl')) 

我得到錯誤:

File "instantbookmark.py", line 3, in <module> 
    getA = requests.get('https://www.instapaper.com/api/add',  auth=HTTPBasicAuth('username', 'password','websiteUrl')) 
TypeError: __init__() takes exactly 3 arguments (4 given) 

我想這是因爲HTTPBasicAuth不能把第三個參數,有沒有人知道一種方法來做到這一點?

+0

這是完整的回溯錯誤 – Wilberto 2013-02-10 12:42:46

+0

是的,我檢查了請求的來源和API的文檔,請參閱下面的答案。 – 2013-02-10 12:43:21

回答

7

HTTPBasicAuth()只有有史以來需要用戶名和密碼參數。沒有第三個參數,完整的。

HTTP基本認證會爲請求添加一個額外的標頭;此信息與GET或POST參數保持分開。當您使用此形式的身份驗證時,您不需要將任何usernamepassword參數傳遞給API方法。

要添加書籤,通過在url參數作爲POST或GET數據參數:

requests.post('https://www.instapaper.com/api/add', data={'url': 'websiteUrl'}, auth=HTTPBasicAuth('username', 'password')) 

無論requests.get()requests.post()會做。