OAuthlib authentication support for Requests.
npx @tessl/cli install tessl/pypi-requests-oauthlib@2.0.0A first-class OAuth library that integrates OAuth 1.0a and OAuth 2.0 authentication support seamlessly with the Requests HTTP library. It provides session-based OAuth clients and authentication classes that handle token management, signature generation, and protocol-specific requirements automatically.
pip install requests-oauthlibfrom requests_oauthlib import OAuth1Session, OAuth2Session, OAuth1, OAuth2, __version__For OAuth 1.0 constants:
from oauthlib.oauth1 import SIGNATURE_HMAC, SIGNATURE_RSA, SIGNATURE_PLAIN
from oauthlib.oauth1 import SIGNATURE_TYPE_AUTH_HEADER, SIGNATURE_TYPE_QUERY, SIGNATURE_TYPE_BODYFor provider-specific compliance fixes:
from requests_oauthlib.compliance_fixes import facebook_compliance_fix, slack_compliance_fixfrom requests_oauthlib import OAuth1Session
# Create OAuth 1 session
oauth = OAuth1Session(
'client_key',
client_secret='client_secret',
callback_uri='https://example.com/callback'
)
# Fetch request token
request_token_url = 'https://api.provider.com/oauth/request_token'
token = oauth.fetch_request_token(request_token_url)
# Get authorization URL
authorization_url = 'https://api.provider.com/oauth/authorize'
auth_url = oauth.authorization_url(authorization_url)
print(f'Please go to {auth_url} and authorize access.')
# After user authorization, fetch access token
access_token_url = 'https://api.provider.com/oauth/access_token'
access_token = oauth.fetch_access_token(access_token_url)
# Make authenticated requests
response = oauth.get('https://api.provider.com/protected_resource')from requests_oauthlib import OAuth2Session
# Create OAuth 2 session
oauth = OAuth2Session(
'client_id',
redirect_uri='https://example.com/callback',
scope=['read', 'write']
)
# Get authorization URL
authorization_url = 'https://api.provider.com/oauth/authorize'
auth_url, state = oauth.authorization_url(authorization_url)
print(f'Please go to {auth_url} and authorize access.')
# Fetch access token using authorization code
token_url = 'https://api.provider.com/oauth/token'
token = oauth.fetch_token(
token_url,
authorization_response='https://example.com/callback?code=AUTH_CODE&state=STATE'
)
# Make authenticated requests
response = oauth.get('https://api.provider.com/protected_resource')The library follows a dual-layer architecture:
OAuth1, OAuth2): Low-level authentication handlers that implement the requests.auth.AuthBase interface for signing individual requestsOAuth1Session, OAuth2Session): High-level workflow managers that extend requests.Session with OAuth-specific methods for token management and the complete authentication flowThis design enables both simple authentication of existing requests and full OAuth workflow management through convenient session-based interfaces.
Comprehensive OAuth 1.0a support including request signing, token workflows, and session management with support for HMAC-SHA1, RSA-SHA1, and PLAINTEXT signature methods.
class OAuth1(requests.auth.AuthBase):
def __init__(
self,
client_key: str,
client_secret: str = None,
resource_owner_key: str = None,
resource_owner_secret: str = None,
callback_uri: str = None,
signature_method: str = SIGNATURE_HMAC,
signature_type: str = SIGNATURE_TYPE_AUTH_HEADER,
rsa_key: str = None,
verifier: str = None,
decoding: str = "utf-8",
client_class = None,
force_include_body: bool = False,
**kwargs
): ...
class OAuth1Session(requests.Session):
def __init__(
self,
client_key: str,
client_secret: str = None,
resource_owner_key: str = None,
resource_owner_secret: str = None,
callback_uri: str = None,
signature_method: str = SIGNATURE_HMAC,
signature_type: str = SIGNATURE_TYPE_AUTH_HEADER,
rsa_key: str = None,
verifier: str = None,
client_class = None,
force_include_body: bool = False,
**kwargs
): ...Full OAuth 2.0 support with automatic token refresh, PKCE extension, compliance hooks, and support for all standard grant types including Authorization Code, Implicit, Resource Owner Password, and Client Credentials.
class OAuth2(requests.auth.AuthBase):
def __init__(
self,
client_id: str = None,
client = None,
token: dict = None
): ...
class OAuth2Session(requests.Session):
def __init__(
self,
client_id: str = None,
client = None,
auto_refresh_url: str = None,
auto_refresh_kwargs: dict = None,
scope: list = None,
redirect_uri: str = None,
token: dict = None,
state = None,
token_updater = None,
pkce: str = None,
**kwargs
): ...Pre-built compliance fixes for popular OAuth providers that implement non-standard OAuth behaviors, enabling seamless integration with services like Facebook, Slack, Instagram, and others.
def facebook_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...
def slack_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...
def instagram_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...
def mailchimp_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...
def fitbit_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...
def weibo_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...
def plentymarkets_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...
def ebay_compliance_fix(session: OAuth2Session) -> OAuth2Session: ...class TokenRequestDenied(ValueError):
def __init__(self, message: str, response): ...
@property
def status_code(self) -> int: ...
class TokenMissing(ValueError):
def __init__(self, message: str, response): ...
class VerifierMissing(ValueError): ...
class TokenUpdated(Warning):
def __init__(self, token: dict): ...
# Package version constant
__version__: str = "2.0.0"# OAuth 1.0 Signature Methods
SIGNATURE_HMAC: str = "HMAC-SHA1"
SIGNATURE_RSA: str = "RSA-SHA1"
SIGNATURE_PLAIN: str = "PLAINTEXT"
# OAuth 1.0 Signature Types
SIGNATURE_TYPE_AUTH_HEADER: str = "AUTH_HEADER"
SIGNATURE_TYPE_QUERY: str = "QUERY"
SIGNATURE_TYPE_BODY: str = "BODY"