Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python requests library HTTPBasicAuth with three parameters

Tags:

python

http

I'm trying to use Instapaper's simple developer api to add a url to my bookmarks using python and the requests library. To authenticate the username and password all works well.

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

But when trying to use the api to add a bookmark:

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

I get error:

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)

I think this is because HTTPBasicAuth can't take a third argument, does anyone know a way to do this?

like image 251
Wilberto Avatar asked Feb 10 '13 12:02

Wilberto


1 Answers

HTTPBasicAuth() only ever takes username and password arguments. There is no 3rd argument, full-stop.

HTTP Basic Authentication adds an extra header to the request; this information is kept separate from the GET or POST parameters. When you use this form of authentication, you do not need to pass any username and password parameters to the API methods either.

To add a bookmark, pass in the url parameter as a POST or GET data parameter:

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

Either requests.get() or requests.post() will do.

like image 136
Martijn Pieters Avatar answered Nov 16 '22 13:11

Martijn Pieters