I'm trying to open URL in Python which needs username and password.
At the beginning there was this error shown:
hereurllib.error.HTTPError: HTTP Error 401: Unauthorized
url = 'http://example.com'
username = 'user'
password = 'password'
passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, url, username, password)
auth_handler = urllib.request.HTTPBasicAuthHandler(passman)
opener = urllib.request.build_opener(auth_handler)
ValueError: AbstractBasicAuthHandler does not support the following scheme: 'Negotiate'
Many web services that require authentication accept HTTP Basic Auth. This is the simplest kind, and Requests supports it straight out of the box.
Making requests with HTTP Basic Auth is very simple:
import requests from requests.auth import HTTPBasicAuth requests.get('http://example.com', auth=HTTPBasicAuth('user', 'pass')) <Response >
In fact, HTTP Basic Auth is so common that Requests provides a handy shorthand for using it:
requests.get('https://api.github.com/user', auth=('user', 'pass')) <Response >
Providing the credentials in a tuple like this is exactly the same as the HTTPBasicAuth example above.