requests-oauthlib: Set OAuth2Session’s Proxy Before fetch_token
Use the same proxy-aware session for token acquisition and protected resources, keeping the existing OAuth flow intact.
from requests_oauthlib import OAuth2Session
session = OAuth2Session(client_id=client_id, redirect_uri=redirect_uri)
session.proxies.update({
'http': 'http://proxy.example:8080',
'https': 'http://proxy.example:8080',
})OAuth2Session extends Requests' Session. Build it with the application's existing client registration, redirect URI, state and scopes, then set its route before the token exchange. For a public HTTP route, a candidate from the HTTP proxy list supplies the address in this session’s proxy map.
Call fetch_token and the protected-resource operations on this session. A proxies argument passed only to fetch_token applies to that exchange; it does not install a lasting route for later calls.
The browser's authorization-page visit uses the browser's route. Python makes the token request separately, so browser success does not configure this session.
Requests' environment integration can affect session proxy selection. For a strict application-owned route, review trust_env; disabling it also requires supplying any needed CA or authentication settings explicitly.
An OAuth error returned by a token endpoint may arrive over a working proxy. Preserve that response separately from proxy authentication, whose credentials are distinct from client secrets and access tokens.