Skip to main content

Capture and store OAuth2 tokens for later use.

Project description

OAuth2 Capture for Django

Store and manage OAuth2 tokens for ongoing API access to social media platforms.

Your Django app needs to post tweets for users. Your CRM creates GitHub repos in client organizations. Your marketing tool schedules LinkedIn posts for customers.

This package captures and manages the OAuth2 tokens that make these use cases possible.

Who This Is For

Building social media management toolsCreating GitHub integrations that act on behalf of usersNeed ongoing API access, not just authentication

Just want social login for your Django app → Use django-allauth instead

Supported Providers

  • Twitter/X - Post tweets, read timelines, manage accounts
  • LinkedIn - Share posts, manage company pages, access connections
  • GitHub - Create repos, manage issues, access organization data
  • Reddit - Post content, read feeds, manage subreddits
  • Pinterest - Create pins, manage boards
  • Facebook - Publish posts, manage pages (limited by Facebook policies)

Planned: Instagram, YouTube, TikTok


What This Package Does vs django-allauth

oauth2_capture solves: "I need my app to post tweets/LinkedIn updates FOR my users"

  • Your app posts to Twitter using Jane's account
  • Your app creates GitHub repos in John's organizations
  • Your app schedules LinkedIn posts for your marketing clients

django-allauth solves: "I need users to log into my site with social accounts"

  • Users click "Login with Google" to access your Django app
  • You create user profiles from their social account data
  • You manage user sessions and permissions

Can you use both? Yes! allauth for login, oauth2_capture for API actions.


Quick Start (5 minutes)

1. Install

uv add git+https://github.com/heysamtexas/django-oauth2-capture.git@master

2. Add to Django Settings

INSTALLED_APPS = [
    ...
    'oauth2_capture',
    ...
]

3. Run Migrations

python manage.py migrate

4. Add URLs

# urls.py
from django.urls import path, include

urlpatterns = [
    ...
    path('oauth2/', include('oauth2_capture.urls')),
    ...
]

5. Verify Setup

python manage.py check oauth2_capture

Expected output: "System check identified no issues"

If error: Check INSTALLED_APPS includes 'oauth2_capture'


Provider Setup

Choose a provider to get started. Each requires different setup steps and credentials.

Twitter (Recommended for beginners)

  1. Create app at developer.twitter.com
  2. Copy Client ID and Secret
  3. Add to settings:
    OAUTH2_CONFIG = {
        "twitter": {
            "client_id": "your_twitter_client_id",
            "client_secret": "your_twitter_client_secret",
            "scope": "tweet.read users.read tweet.write offline.access",
            "code_verifier": "challenge",  # Required for Twitter
        }
    }
    
  4. Test: Visit /oauth2/twitter/connect/ - should redirect to Twitter

GitHub (For development tools)

  1. Go to GitHub Settings → Developer settings → OAuth Apps
  2. Create new OAuth App with redirect URI: http://localhost:8000/oauth2/github/callback/
  3. Add to settings:
    OAUTH2_CONFIG = {
        "github": {
            "client_id": "your_github_client_id",
            "client_secret": "your_github_client_secret",
            "scope": "user repo",  # Minimal scopes to start
        }
    }
    

LinkedIn (For business applications)

  1. Create app at LinkedIn Developer Portal
  2. Request necessary permissions (may require approval)
  3. Add to settings:
    OAUTH2_CONFIG = {
        "linkedin": {
            "client_id": "your_linkedin_client_id",
            "client_secret": "your_linkedin_client_secret",
            "scope": "profile email openid w_member_social",
        }
    }
    

Usage

1. Create Connection Links

<a href="{% url 'oauth2_capture:initiate_oauth2' 'twitter' %}">Connect Twitter</a>
<a href="{% url 'oauth2_capture:initiate_oauth2' 'github' %}">Connect GitHub</a>

2. Access Tokens in Your Views

from oauth2_capture.models import OAuth2Token

def my_view(request):
    # Get a user's GitHub token
    token = OAuth2Token.objects.filter(
        owner=request.user,
        provider='github'
    ).first()

    if token:
        # Use the token to make API requests
        access_token = token.access_token
        # ...

For more detailed examples, check out the demo app in the development/ directory of the repository, which shows complete implementation examples for all supported providers.


Adding a new provider

class NewProviderOAuth2Provider(OAuth2Provider):
    @property
    def authorize_url(self) -> str:
        return "https://newprovider.com/oauth/authorize"

    @property
    def token_url(self) -> str:
        return "https://newprovider.com/oauth/token"

    @property
    def user_info_url(self) -> str:
        return "https://api.newprovider.com/userinfo"

    def get_user_info(self, access_token: str) -> dict:
        headers = {"Authorization": f"Bearer {access_token}"}
        response = requests.get(self.user_info_url, headers=headers, timeout=10)
        return response.json()

    def exchange_code_for_token(self, code: str, redirect_uri: str) -> dict:
        data = {
            "grant_type": "authorization_code",
            "code": code,
            "redirect_uri": redirect_uri,
            "client_id": self.config["client_id"],
            "client_secret": self.config["client_secret"],
        }
        headers = {"Content-Type": "application/x-www-form-urlencoded"}
        response = requests.post(self.token_url, data=data, headers=headers, timeout=10)
        return response.json()

User Flow

The following is a simplified user flow for how we obtain and save the oauth tokens. The flow is as follows:

sequenceDiagram
    participant U as User
    participant C as Client
    participant P as Provider
    U->>C: Clicks on "connect" with provider
    C->>P: Redirects to provider's authorize URL
    P->>C: Redirects to client's redirect URI with code
    C->>P: Exchanges code for token
    P->>C: Returns token
    C->>P: Retrieves user info
    P->>C: Returns user info
    C->>U: Redirects to home page
    C->>C: Save or update the user token in database

Note: if the user is not logged in at their provider, they will be prompted to login before they can authorize the client. (This is not shown in the diagram)


Setting up your development environment

  1. Clone the repository
  2. Create a virtual environment
  3. Install the requirements
  4. Configure the environment variables
    1. Acquire the client ID and client secret from the providers
    2. Put client_id and secrets into local env file in the development/ folder
  5. Migrate the database with python manage.py migrate

Setup each provider by following the instructions in the docs/ folder.


License


Appendix

Provider's endpoints, docs, etc

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

oauth2_capture-0.5.1.tar.gz (211.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

oauth2_capture-0.5.1-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file oauth2_capture-0.5.1.tar.gz.

File metadata

  • Download URL: oauth2_capture-0.5.1.tar.gz
  • Upload date:
  • Size: 211.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for oauth2_capture-0.5.1.tar.gz
Algorithm Hash digest
SHA256 af8cc018ed969198cf5905b75d668e20124e62ed47e455e33c237c6e835ce22a
MD5 e157cf1880ff70604e8b080dc4865f3e
BLAKE2b-256 aa8787701b5d50ac90a57c59ec2fa068d2e105e3702d76c83bbf620ae9031bf6

See more details on using hashes here.

Provenance

The following attestation bundles were made for oauth2_capture-0.5.1.tar.gz:

Publisher: publish.yml on heysamtexas/django-oauth2-capture

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file oauth2_capture-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: oauth2_capture-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for oauth2_capture-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2dabe28050debffe55f6eca42c455356617bbc1831134c005c57e24d232379b3
MD5 b96a1c6481ce21cb0a41cb0cb75f7e0c
BLAKE2b-256 eed83842379e8902e645787c25b1a2ccdfa6fce8973a96b45ede985be1cbfe61

See more details on using hashes here.

Provenance

The following attestation bundles were made for oauth2_capture-0.5.1-py3-none-any.whl:

Publisher: publish.yml on heysamtexas/django-oauth2-capture

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page