Zombistic Zombistic - 2 years ago 746
Python Question

AbstractBasicAuthHandler does not support the following scheme: 'Negotiate' [Python]

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

Following tutorials I modded my script to look like this:

import urllib.request
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)

After that the error disappeared, but another appeared in its place:

ValueError: AbstractBasicAuthHandler does not support the following scheme: 'Negotiate'

Anybody knows what could be the problem?

Answer Source

Switch to requests

Basic Authentication

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 [200]>

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 [200]>

Providing the credentials in a tuple like this is exactly the same as the HTTPBasicAuth example above.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download