oauth2 - OAuth 1.0a Client

1.9.0.post1 · deprecated · verified Sun Apr 12

The `oauth2` library provides a Python implementation for the OAuth 1.0a protocol. While it remains functional for applications requiring this older standard, OAuth 1.0a has largely been superseded by OAuth 2.0 for modern API integrations. The library's last significant development occurred around 2015, with minor updates up to 2022, and the current PyPI version is 1.9.0.post1.

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize an OAuth 1.0a client with consumer and token credentials (assuming they are pre-obtained), then use it to make a signed GET request to a protected resource. This is typical for 2-legged OAuth or after the 3-legged flow has completed and access tokens are available. The example uses environment variables for sensitive credentials.

import oauth2
import os
from urllib.parse import parse_qsl

# Replace with your actual consumer and token keys/secrets (from environment or config)
CONSUMER_KEY = os.environ.get('OAUTH2_CONSUMER_KEY', 'your_consumer_key')
CONSUMER_SECRET = os.environ.get('OAUTH2_CONSUMER_SECRET', 'your_consumer_secret')
TOKEN_KEY = os.environ.get('OAUTH2_TOKEN_KEY', 'your_token_key')
TOKEN_SECRET = os.environ.get('OAUTH2_TOKEN_SECRET', 'your_token_secret')

# The URL to make a signed request to
REQUEST_URL = "http://example.com/api/resource"

# --- Step 1: Initialize Consumer and Token ---
# Create a Consumer object (application credentials)
consumer = oauth2.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)

# Create a Token object (user credentials obtained previously via 3-legged flow)
token = oauth2.Token(key=TOKEN_KEY, secret=TOKEN_SECRET)

# --- Step 2: Create an OAuth2 Client ---
# The client combines consumer and token to sign requests
client = oauth2.Client(consumer, token)

# --- Step 3: Make a signed request ---
print(f"Making a signed GET request to: {REQUEST_URL}")
try:
    resp, content = client.request(REQUEST_URL, "GET")

    print(f"\nHTTP Status: {resp.status}")
    print(f"Response Content (first 200 chars): {content.decode('utf-8')[:200]}...")

    if resp.status != 200:
        print(f"Error: {content.decode('utf-8')}")

except Exception as e:
    print(f"An error occurred during the request: {e}")

# --- Example: Initiating a 3-legged OAuth flow (getting a request token) ---
# This part assumes a request token URL exists for demonstration.
# request_token_url = "http://example.com/oauth/request_token"
# print(f"\nAttempting to get a request token from: {request_token_url}")
# try:
#     # For requesting a request token, usually only the consumer is needed initially
#     req_client = oauth2.Client(consumer)
#     resp_req, content_req = req_client.request(request_token_url, "GET")
#     if resp_req.status == 200:
#         request_token_data = dict(parse_qsl(content_req.decode('utf-8')))
#         print(f"Successfully got Request Token: {request_token_data}")
#     else:
#         print(f"Failed to get Request Token: Status {resp_req.status}, Content: {content_req.decode('utf-8')}")
# except Exception as e:
#     print(f"An error occurred getting request token: {e}")

view raw JSON →