Configure both gspread's authorized API session and the credential-refresh request transport when either needs the proxy:

import requests
import gspread
from google.auth.transport.requests import AuthorizedSession, Request

routes = {
    'http': 'http://proxy.example:8080',
    'https': 'http://proxy.example:8080',
}
token_session = requests.Session()
token_session.proxies.update(routes)

session = AuthorizedSession(
    credentials,
    auth_request=Request(session=token_session),
)
session.proxies.update(routes)
client = gspread.Client(auth=credentials, session=session)

This uses gspread 6's supplied-session Client interface with the application's existing credentials. It configures transport, not spreadsheet permissions or OAuth scopes. For public API and token endpoints on a permitted public route, choose an unauthenticated HTTP candidate from the HTTP proxy list for the routes map. Keep both sessions free of any retained proxy account when changing the intermediary.

AuthorizedSession adds credentials to API requests and can refresh them. Without auth_request, google-auth can create another Requests session for that traffic. Proxy defaults on the authorized session do not configure an independently created one.

The Request adapter above supplies that second route deliberately. Retain both sessions while used, then close externally owned sessions at the end of their application scope.

These are Session defaults, so Requests environment merging can still affect them. Account for inherited proxy inputs when a fixed route is required.

Use the failed URL to distinguish a spreadsheet refusal, proxy challenge and credential-endpoint failure. A cached access token can make API calls succeed without exercising refresh at all.

Ũhoro ũhĩtĩte