Rauth

0.7.3 · abandoned · verified Mon Apr 13

Rauth is a Python library that provides consumer support for OAuth 1.0/a, OAuth 2.0, and Ofly, built on top of the popular Requests library. Its last release, version 0.7.3, was in January 2017, indicating it is no longer actively maintained.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic OAuth 2.0 authorization code flow using Rauth. It simulates a web application by prompting the user to manually visit an authorization URL and then input the received authorization code to obtain an access token and make an authenticated API call. Remember to replace placeholder URLs and credentials with actual values for your OAuth provider.

import os
import webbrowser
from rauth import OAuth2Service

# --- Configuration (Replace with your actual app details and environment variables) ---
CLIENT_ID = os.environ.get('RAUTH_CLIENT_ID', 'your_client_id')
CLIENT_SECRET = os.environ.get('RAUTH_CLIENT_SECRET', 'your_client_secret')
REDIRECT_URI = 'http://localhost:8000/callback'

# Example for a hypothetical OAuth 2.0 provider
service = OAuth2Service(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    name='example_provider',
    authorize_url='https://example.com/oauth/authorize',
    access_token_url='https://example.com/oauth/token',
    base_url='https://example.com/api/'
)

print('--- Starting OAuth 2.0 Flow ---')

# Step 1: Get the authorization URL and redirect the user
params = {
    'redirect_uri': REDIRECT_URI,
    'response_type': 'code',
    'scope': 'read_profile'
}
authorize_url = service.get_authorize_url(**params)

print(f"Please visit this URL in your browser:\n{authorize_url}")
webbrowser.open(authorize_url)

# In a real web application, this 'code' would come from the redirect_uri callback
# For this example, we simulate getting the code from user input after manual authorization.
authorization_code = input('Enter the authorization code from the redirect URL: ')

# Step 2: Exchange the authorization code for an access token
data = {
    'code': authorization_code,
    'grant_type': 'authorization_code',
    'redirect_uri': REDIRECT_URI
}
session = service.get_auth_session(data=data, decoder=lambda x: x.json())

# Step 3: Make an authenticated request
try:
    response = session.get('user/profile') # Example API endpoint
    response.raise_for_status() # Raise an exception for HTTP errors
    user_profile = response.json()
    print(f"Successfully fetched user profile: {user_profile}")
except Exception as e:
    print(f"Error during API request: {e}")
    if hasattr(e, 'response') and e.response is not None:
        print(f"Response content: {e.response.text}")

print('--- OAuth 2.0 Flow Complete ---')

view raw JSON →