--- 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" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %}

Scopes

{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}

scope_str[source]

scope_str(*scopes)

Convert scopes into a comma-separated string

{% endraw %} {% raw %}
{% endraw %} {% raw %}
scope_str(Scope.repo,Scope.admin_public_key,Scope.public_repo)
'repo,admin:public_key,public_repo'
{% endraw %} {% raw %}
{% endraw %} {% raw %}

class GhDeviceAuth[source]

GhDeviceAuth(client_id='771f3c3af93face45f52', *scopes) :: GetAttrBase

Get an oauth token using the GitHub API device flow

{% endraw %} {% raw %}
{% endraw %}

Creating a GhDeviceAuth will complete the first step in the GitHub API device flow, getting device and user codes.

{% raw %}
ghauth = GhDeviceAuth()
ghauth.device_code,ghauth.user_code
('4a862fd5d474b58cfad0e2432f47c33a2976facc', '28F1-E3A6')
{% endraw %} {% raw %}

GhDeviceAuth.url_docs[source]

GhDeviceAuth.url_docs()

Default instructions on how to authenticate

{% endraw %} {% raw %}
{% endraw %}

You can provide your own instructions on how to authenticate, or just print this out:

{% raw %}
print(ghauth.url_docs())
First copy your one-time code: 28F1-E3A6
Then visit https://github.com/login/device in your browser, and paste the code when prompted.
{% endraw %} {% raw %}

GhDeviceAuth.open_browser[source]

GhDeviceAuth.open_browser()

Open a web browser with the verification URL

{% endraw %} {% raw %}
{% endraw %}

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.

{% raw %}

GhDeviceAuth.auth[source]

GhDeviceAuth.auth()

Return token if authentication complete, or None otherwise

{% endraw %} {% raw %}
{% endraw %}

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.

{% raw %}
print(ghauth.auth())
None
{% endraw %} {% raw %}

GhDeviceAuth.wait[source]

GhDeviceAuth.wait(cb:callable=None, n_polls=9999)

Wait up to n_polls times for authentication to complete, calling cb after each poll, if passed

{% endraw %} {% raw %}
{% endraw %}

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=''))