Package: ghtoken
GitHub|PyPI|IssuesWhen writing a Python program for interacting with GitHub’s API, you’ll likely
want to use the local user’s GitHub access token for authentication.  Asking
the user to provide the token every time is undesirable, so you’d rather look
up the token in some well-known storage location.  The problem is, there have
been so many places to store GitHub tokens supported by different programs over
the years, and asking your users to migrate to a new one shouldn’t be
necessary.That’s whereghtokencomes in: it provides a single function for checking
multiple well-known sources for GitHub access tokens plus separate functions
for querying individual sources in case you need to combine the queries in
novel ways.The following token sources are currently supported:.envfilesenvironment variablestheghcommandthehubconfiguration filethehub.oauthtokenGit config optionInstallationghtokenrequires Python 3.7 or higher.  Just usepipfor Python 3 (You have pip, right?) to install it:python3 -m pip install ghtokenAPINote: When retrieving GitHub access tokens, no validation of the token format
is performed other than rejecting empty strings.ghtoken.get_ghtoken(*,dotenv:bool|str|os.PathLike[str]=True,environ:bool=True,gh:bool=True,hub:bool=True,hub_oauthtoken:bool=True,)->strRetrieve a locally-stored GitHub access token by checking various sources
(listed below) and returning the first token found.  Individual sources can be
disabled by setting the corresponding keyword argument toFalse.The sources are as follows, listed in the order in which they are consulted:dotenvLook for a.envfile by searching from the current directory upwards
and reading the first file found.  Alternatively, a specific file may be
read by setting thedotenvkeyword argument to the path to the file.
If the file (whether found by searching or explicitly specified) cannot be
read or parsed, control proceeds to the next enabled source.If the file contains aGH_TOKEN=...orGITHUB_TOKEN=...assignment
with a non-empty value, that value is returned; otherwise, control proceeds
to the next enabled source.  If assignments for both keys are present,GH_TOKENtakes precedence overGITHUB_TOKEN.Reading values from a.envfile will not modify the process’s
environment variables.environIf the environment variableGH_TOKENorGITHUB_TOKENis set to a
nonempty string, that variable’s value is returned; otherwise, control
proceeds to the next enabled source.  If both environment variables are
set,GH_TOKENtakes precedence overGITHUB_TOKEN.ghRetrieve a GitHub access token stored by theghcommand by runninggh auth token--hostnamegithub.com.  If the command fails or outputs an
empty line, control proceeds to the next enabled source.Note that, if gh has stored its access token in a system keyring, the user
may be prompted to unlock the keyring.Note that, if theGH_TOKENorGITHUB_TOKENenvironment variable is
set to a nonempty string, gh will return the value of the envvar rather
than returning whatever access token may already be stored, and this
happens even if theenvironargument toget_ghtoken()isFalse.hubRetrieve a GitHub access token from thehubconfiguration file.  If the
configuration file does not exist or is malformed, or if the file does not
contain a nonempty token for thegithub.comdomain, control proceeds to
the next enabled source.hub_oauthtokenRetrieve a GitHub access token from the Git config keyhub.oauthtoken,
used by thegit-hubprogram.  If the key is set to the empty string, or if
the Git config keyhub.baseurlis set to a value other thanhttps://api.github.com, control proceeds to the next enabled source.Git config values are retrieved by runninggit config--get....  If
such a command fails, control proceeds to the next enabled source.If thehub.oauthtokenvalue starts with!, the rest of the value is
executed as a shell command, and the command’s standard output (with
leading & trailing whitespace stripped) is returned.If no enabled source returns a token, then aghtoken.GHTokenNotFoundexception is raised.ghtoken.ghtoken_from_dotenv(path:str|os.PathLike[str]|None=None)->strRetrieve a GitHub access token from an.envfile as described above.  A
path to a specific file may be supplied.  If no token is found, aghtoken.GHTokenNotFoundexception is raised.ghtoken.ghtoken_from_environ()->strRetrieve a GitHub access token from environment variables as described above.
If no token is found, aghtoken.GHTokenNotFoundexception is raised.ghtoken.ghtoken_from_gh()->strRetrieve a GitHub access token from theghcommand as described above.  If
no token is found, aghtoken.GHTokenNotFoundexception is raised.ghtoken.ghtoken_from_hub()->strRetrieve a GitHub access token from thehubconfiguration file as described
above.  If no token is found, aghtoken.GHTokenNotFoundexception is
raised.ghtoken.ghtoken_from_hub_oauthtoken()->strRetrieve a GitHub access token from thehub.oauthtokenGit config option as
described above.  If no token is found, aghtoken.GHTokenNotFoundexception
is raised.Commandghtokenalso provides a command of the same name for looking up a GitHub
token from the command line:ghtoken [<options>]ghtokenretrieves the local user’s GitHub access token from local storage
and prints it.  If no token can be found, a message is printed to standard
error, and the command exits with a nonzero status.Options-EFILE,--envFILEUse the given file as the.envfile source--no-dotenvDo not consult a.envfile--no-environDo not consult environment variables--no-ghDo not consultgh--no-hubDo not consulthubconfiguration file--no-hub-oauthtokenDo not consulthub.oauthtokenGit config
option