Open stphnthiel opened 1 year ago
This docs page on AuthHandlers misses one line of code to be working
AuthHandlers
import requests from requests_toolbelt.auth.handler import AuthHandler def gitlab_auth(request): request.headers['PRIVATE-TOKEN'] = 'asecrettoken' return request --> add this line! handler = AuthHandler({ 'https://api.github.com': ('sigmavirus24', 'apassword'), 'https://gitlab.com': gitlab_auth, }) session = requests.Session() session.auth = handler r = session.get('https://api.github.com/user') # assert r.ok r2 = session.get('https://gitlab.com/api/v3/projects') # assert r2.ok
The current example gives the following error:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [17], in <cell line: 1>() ----> 1 r = session.get("some-url") File python3.8/site-packages/requests/sessions.py:555, in Session.get(self, url, **kwargs) 547 r"""Sends a GET request. Returns :class:`Response` object. 548 549 :param url: URL for the new :class:`Request` object. 550 :param \*\*kwargs: Optional arguments that ``request`` takes. 551 :rtype: requests.Response 552 """ 554 kwargs.setdefault('allow_redirects', True) --> 555 return self.request('GET', url, **kwargs) File python3.8/site-packages/requests_toolbelt/sessions.py:64, in BaseUrlSession.request(self, method, url, *args, **kwargs) 62 """Send the request after generating the complete URL.""" 63 url = self.create_url(url) ---> 64 return super(BaseUrlSession, self).request( 65 method, url, *args, **kwargs 66 ) File python3.8/site-packages/requests/sessions.py:528, in Session.request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json) 515 # Create the Request. 516 req = Request( 517 method=method.upper(), 518 url=url, (...) 526 hooks=hooks, 527 ) --> 528 prep = self.prepare_request(req) 530 proxies = proxies or {} 532 settings = self.merge_environment_settings( 533 prep.url, proxies, stream, verify, cert 534 ) File python3.8/site-packages/requests/sessions.py:456, in Session.prepare_request(self, request) 453 auth = get_netrc_auth(request.url) 455 p = PreparedRequest() --> 456 p.prepare( 457 method=request.method.upper(), 458 url=request.url, 459 files=request.files, 460 data=request.data, 461 json=request.json, 462 headers=merge_setting(request.headers, self.headers, dict_class=CaseInsensitiveDict), 463 params=merge_setting(request.params, self.params), 464 auth=merge_setting(auth, self.auth), 465 cookies=merged_cookies, 466 hooks=merge_hooks(request.hooks, self.hooks), 467 ) 468 return p File python3.8/site-packages/requests/models.py:320, in PreparedRequest.prepare(self, method, url, headers, files, data, params, auth, cookies, hooks, json) 318 self.prepare_cookies(cookies) 319 self.prepare_body(data, files, json) --> 320 self.prepare_auth(auth, url) 322 # Note that prepare_auth must be last to enable authentication schemes 323 # such as OAuth to work on a fully prepared request. 324 325 # This MUST go after prepare_auth. Authenticators could add a hook 326 self.prepare_hooks(hooks) File python3.8/site-packages/requests/models.py:559, in PreparedRequest.prepare_auth(self, auth, url) 556 r = auth(self) 558 # Update self to reflect the auth changes. --> 559 self.__dict__.update(r.__dict__) 561 # Recompute Content-Length 562 self.prepare_content_length(self.body) AttributeError: 'NoneType' object has no attribute '__dict__'
Including that here so people find it when Googling.
This docs page on
AuthHandlers
misses one line of code to be workingThe current example gives the following error:
Including that here so people find it when Googling.