Install & Compatibility
Where this runs
tested against v1.9.0.post1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.720s · 20.3MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.7s · import 0.584s · 21MB
18MB installed
● package 18MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Consumer
✓ from oauth2 import Consumer
✗ from oauth2 import Consumer
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}")
Upgrade
Version history
1.9.0.post1latest on PyPI · released Sep 12, 2015
Audit
Dependencies
httplib2requiredRequired HTTP client for making signed requests.