--- title: Authentication keywords: fastai sidebar: home_sidebar summary: "Helpers for creating GitHub API tokens" description: "Helpers for creating GitHub API tokens" nb_path: "02_auth.ipynb" ---
scope_str(Scope.repo,Scope.admin_public_key,Scope.public_repo)
Creating a GhDeviceAuth
will complete the first step in the GitHub API device flow, getting device and user codes.
ghauth = GhDeviceAuth()
ghauth.device_code,ghauth.user_code
You can provide your own instructions on how to authenticate, or just print this out:
print(ghauth.url_docs())
This uses Python's webbrowser.open
, which will use the user's default web browser. This won't work well if the user is using a remote terminal.
Until the user has completed authentication in the browser, this will return None. Normally, you won't call this directly, but will call wait
(see below), which will repeatedly call auth
until authentication is complete.
print(ghauth.auth())
If you pass a callback to cb
, it will be called after each unsuccessful check for user authentication. For instance, to print a .
to the screen after each poll, and store the token in a variable token
when complete, you could use:
token = ghauth.wait(lambda: print('.', end=''))
When we run this we'll be shown a URL to visit and a code to enter in order to authenticate. Normally we'll be prompted to open a browser, and the function will wait for authentication to complete -- for demonstrating here we'll skip both of these steps:
github_auth_device('n',n_polls=0)