CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-pygithub

Comprehensive Python client library providing full access to the GitHub REST API v3 for programmatic management of repositories, users, organizations, issues, pull requests, and other GitHub entities

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

authentication.mddocs/

Authentication

PyGithub provides comprehensive authentication support for all GitHub API access patterns including personal access tokens, GitHub Apps, OAuth, and legacy authentication methods.

Capabilities

Personal Access Token Authentication

The most common authentication method using GitHub personal access tokens. Supports both classic and fine-grained personal access tokens.

class Auth:
    class Token:
        def __init__(self, token: str):
            """
            Create token-based authentication.
            
            Args:
                token (str): GitHub personal access token
            """

Usage example:

from github import Github, Auth

# Using personal access token
auth = Auth.Token("your_personal_access_token")
g = Github(auth=auth)

# Now you can make authenticated requests
user = g.get_user()  # Gets the authenticated user
print(f"Authenticated as: {user.login}")

GitHub App Authentication

Authentication for GitHub Apps using private key or JWT tokens. Supports both app-level authentication and installation-specific authentication.

class Auth:
    class AppAuth:
        def __init__(self, app_id: str, private_key: str):
            """
            GitHub App authentication using private key.
            
            Args:
                app_id (str): GitHub App ID
                private_key (str): Private key content or path to key file
            """
    
    class AppAuthToken:
        def __init__(self, token: str):
            """
            GitHub App authentication using a single constant JWT token.
            
            Args:
                token (str): Pre-generated JWT token
            """
    
    class AppInstallationAuth:
        def __init__(
            self,
            app_auth: AppAuth,
            installation_id: int,
            token_permissions: dict[str, str] | None = None
        ):
            """
            GitHub App installation authentication.
            
            Args:
                app_auth (AppAuth): GitHub App authentication object
                installation_id (int): Installation ID
                token_permissions (dict, optional): Requested permissions for the token
            """
    
    class AppUserAuth:
        def __init__(
            self,
            client_id: str,
            client_secret: str,
            token: str,
            token_type: str = None,
            expires_at: datetime = None,
            refresh_token: str = None,
            refresh_expires_at: datetime = None
        ):
            """
            GitHub App user-to-server authentication for acting on behalf of a user.
            
            Args:
                client_id (str): OAuth app client ID
                client_secret (str): OAuth app client secret
                token (str): User access token
                token_type (str, optional): Token type (usually "bearer")
                expires_at (datetime, optional): Token expiration time
                refresh_token (str, optional): Refresh token for renewing access
                refresh_expires_at (datetime, optional): Refresh token expiration
            """
    
    class NetrcAuth:
        def __init__(self):
            """
            Authentication using .netrc file credentials.
            Automatically reads credentials from .netrc file for the GitHub hostname.
            """

Usage example:

from github import Github, Auth

# App-level authentication (for app management operations)
app_auth = Auth.AppAuth("123456", private_key_content)
g = Github(auth=app_auth)

# Installation-specific authentication (for repository operations)
installation_auth = Auth.AppInstallationAuth(
    installation_id=12345678,
    app_id="123456", 
    private_key=private_key_content
)
g = Github(auth=installation_auth)

# Access repositories the app is installed on
for repo in g.get_installation(12345678).get_repos():
    print(f"App has access to: {repo.full_name}")

Additional Authentication Methods

Additional authentication methods for specialized use cases.

# Pre-generated JWT token authentication
auth_token = Auth.AppAuthToken("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9...")
g = Github(auth=auth_token)

# .netrc file authentication  
netrc_auth = Auth.NetrcAuth()
g = Github(auth=netrc_auth)

Legacy Authentication Methods

Legacy authentication methods maintained for backward compatibility.

class Auth:
    class Login:
        def __init__(self, username: str, password: str):
            """
            Username/password authentication (deprecated).
            
            Args:
                username (str): GitHub username
                password (str): GitHub password or personal access token
            """
    
    class NetrcAuth:
        def __init__(self):
            """
            Authentication using .netrc file credentials.
            """

GitHub Integration Client

Specialized client for GitHub App integration management operations.

class GithubIntegration:
    def __init__(
        self,
        integration_id: str,
        private_key: str,
        base_url: str = "https://api.github.com"
    ):
        """
        GitHub App integration client.
        
        Args:
            integration_id (str): GitHub App ID
            private_key (str): Private key content or path
            base_url (str): GitHub API base URL
        """
    
    def get_installation(self, owner: str, repo: str = None):
        """
        Get installation for owner/repo or organization.
        
        Args:
            owner (str): Repository owner or organization name
            repo (str, optional): Repository name
            
        Returns:
            Installation: Installation object
        """
    
    def get_installations(self):
        """
        Get all installations for this GitHub App.
        
        Returns:
            PaginatedList[Installation]: List of installations
        """
    
    def get_access_token(self, installation_id: int, permissions: dict = None):
        """
        Get installation access token.
        
        Args:
            installation_id (int): Installation ID
            permissions (dict, optional): Requested permissions
            
        Returns:
            InstallationAuthorization: Access token object
        """
    
    def create_jwt(self, expiration: int = None):
        """
        Create JWT token for app authentication.
        
        Args:
            expiration (int, optional): Token expiration in seconds
            
        Returns:
            str: JWT token
        """

Usage example for GitHub App integrations:

from github import GithubIntegration

# Create integration client
integration = GithubIntegration("123456", private_key_content)

# Get installation for a specific repository
installation = integration.get_installation("owner", "repo")
print(f"Installation ID: {installation.id}")

# Get access token for the installation
auth = integration.get_access_token(installation.id)
token = auth.token

# Use the token to create authenticated Github client
from github import Github, Auth
g = Github(auth=Auth.Token(token))

Legacy App Authentication

Legacy GitHub App authentication class maintained for backward compatibility.

class AppAuthentication:
    def __init__(
        self,
        app_id: str,
        private_key: str,
        installation_id: int = None
    ):
        """
        Legacy GitHub App authentication (deprecated).
        
        Args:
            app_id (str): GitHub App ID
            private_key (str): Private key content
            installation_id (int, optional): Installation ID
        """

Authentication Patterns

Repository Access Pattern

from github import Github, Auth

# Personal access token for individual user
auth = Auth.Token("your_token")
g = Github(auth=auth)

# Access user's repositories
user = g.get_user()
for repo in user.get_repos():
    print(f"User repo: {repo.full_name}")

# Access public repositories
repo = g.get_repo("octocat/Hello-World")
print(f"Public repo: {repo.description}")

Organization Access Pattern

from github import Github, Auth

# App installation for organization access
auth = Auth.AppInstallationAuth(
    installation_id=12345678,
    app_id="123456",
    private_key=private_key_content
)
g = Github(auth=auth)

# Access organization repositories
org = g.get_organization("myorg")
for repo in org.get_repos():
    print(f"Org repo: {repo.full_name}")

Error Handling

from github import Github, Auth, BadCredentialsException, TwoFactorException

try:
    auth = Auth.Token("invalid_token")
    g = Github(auth=auth)
    user = g.get_user()
except BadCredentialsException:
    print("Invalid credentials provided")
except TwoFactorException:
    print("Two-factor authentication required")

Install with Tessl CLI

npx tessl i tessl/pypi-pygithub

docs

authentication.md

git-operations.md

index.md

issues-pull-requests.md

repository-management.md

search-discovery.md

user-organization-management.md

workflows-actions.md

tile.json