Skip to main content

github-fine-grained-token-client

pipeline status docs pypi supported python versions

Library and CLI tool for creating and managing fine-grained GitHub tokens.

Purpose

GitHub allows the creation of per-project access tokens with fine-grained permissions but doesn't currently have an API to do so.

This tool seeks to provide a client exposing this functionality anyway by whatever means necessary. More specifically, for now this means simulating requests to the relevant parts of the web interface closely enough to how a browser would perform them.

Installation

To install from PyPI:

pip3 install github-fine-grained-token-client

Command-line tool usage

To use the CLI tool, you'll need to install some optional dependencies first:

pip3 install 'github-fine-grained-token-client[cli]'

To create a token yourtokenname with read-only permissions (the default) for your public GitHub repository yourrepo:

github-fine-grained-token-client create --repositories yourrepo yourtokenname

There are more commands and options - please refer to the docs.

Usage as a library

Basic example script:

import asyncio
from datetime import timedelta
from os import environ

from github_fine_grained_token_client import (
    BlockingPromptTwoFactorOtpProvider,
    GithubCredentials,
    SelectRepositories,
    async_client,
)

credentials = GithubCredentials(environ["GITHUB_USER"], environ["GITHUB_PASS"])
assert credentials.username and credentials.password


async def main() -> str:
    async with async_client(
        credentials=credentials,
        # 2FA will be mandatory on GitHub starting at some point in 2023
        two_factor_otp_provider=BlockingPromptTwoFactorOtpProvider(),
    ) as session:
        token = await session.create_token(
            "my token",
            expires=timedelta(days=364),
            scope=SelectRepositories(["my-project"]),
        )
    return token


token = asyncio.run(main())

print(token)

More information

For more detailed usage information and the API reference, refer to the documentation.

License

MIT License, see LICENSE file.

Download files

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

Source Distribution

github_fine_grained_token_client-1.0.0.tar.gz (22.8 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file github_fine_grained_token_client-1.0.0.tar.gz.

File metadata

File hashes

Hashes for github_fine_grained_token_client-1.0.0.tar.gz
Algorithm Hash digest
SHA256 237a2880b9af09f2594fdda18b89263fcaa7cc56b2f58f5877467721aa677873
MD5 afcabbc534bed4410a6235aa23fd451d
BLAKE2b-256 02c785b2f149702597d5bef6f6e9758093d510cfe67b72064b6193ba9a47736d

See more details on using hashes here.

File details

Details for the file github_fine_grained_token_client-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for github_fine_grained_token_client-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5936e08eecec2d3d13ddc00ee1357f83dcc2892b8bf067495255b45157ab88ad
MD5 5ea0fdd0e13f5db402cd0fd760bd2897
BLAKE2b-256 bd7548c27c328e7acf3ed6999a235aa2f642ef69fc1d4911bf9f35eee9355ccb

See more details on using hashes here.

Supported by

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