# Authentication


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Scopes

------------------------------------------------------------------------

<a href="https://github.com/fastai/ghapi/blob/main/ghapi/auth.py#L29"
target="_blank" style="float:right; font-size:smaller">source</a>

### scope_str

``` python

def scope_str(
    scopes:VAR_POSITIONAL
)->str:

```

*Convert `scopes` into a comma-separated string*

``` python
scope_str(Scope.repo,Scope.admin_public_key,Scope.public_repo)
```

    'repo,admin:public_key,public_repo'

------------------------------------------------------------------------

<a href="https://github.com/fastai/ghapi/blob/main/ghapi/auth.py#L37"
target="_blank" style="float:right; font-size:smaller">source</a>

### GhDeviceAuth

``` python

def GhDeviceAuth(
    client_id:str='71604a89b882ab8c8634', scopes:VAR_POSITIONAL
):

```

*Get an oauth token using the GitHub API device flow*

Creating a
[`GhDeviceAuth`](https://ghapi.fast.ai/auth.html#ghdeviceauth) will
complete the first step in the GitHub API device flow, getting device
and user codes.

``` python
ghauth = GhDeviceAuth()
ghauth.device_code,ghauth.user_code
```

    ('62956bc850018fb2e5c4b62501df72bbe5583a5a', '247D-B1A6')

------------------------------------------------------------------------

<a href="https://github.com/fastai/ghapi/blob/main/ghapi/auth.py#L49"
target="_blank" style="float:right; font-size:smaller">source</a>

### GhDeviceAuth.url_docs

``` python

def url_docs(
    
)->str:

```

*Default instructions on how to authenticate*

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

``` python
print(ghauth.url_docs())
```

    First copy your one-time code: 247D-B1A6
    Then visit https://github.com/login/device in your browser, and paste the code when prompted.

------------------------------------------------------------------------

<a href="https://github.com/fastai/ghapi/blob/main/ghapi/auth.py#L56"
target="_blank" style="float:right; font-size:smaller">source</a>

### GhDeviceAuth.open_browser

``` python

def open_browser(
    
):

```

*Open a web browser with the verification URL*

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.

------------------------------------------------------------------------

<a href="https://github.com/fastai/ghapi/blob/main/ghapi/auth.py#L62"
target="_blank" style="float:right; font-size:smaller">source</a>

### GhDeviceAuth.auth

``` python

def auth(
    
)->str:

```

*Return token if authentication complete, or `None` otherwise*

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.

``` python
print(ghauth.auth())
```

    None

------------------------------------------------------------------------

<a href="https://github.com/fastai/ghapi/blob/main/ghapi/auth.py#L75"
target="_blank" style="float:right; font-size:smaller">source</a>

### GhDeviceAuth.wait

``` python

def wait(
    cb:callable=None, n_polls:int=9999
)->str:

```

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

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:

``` python
token = ghauth.wait(lambda: print('.', end=''))
```

------------------------------------------------------------------------

<a href="https://github.com/fastai/ghapi/blob/main/ghapi/auth.py#L86"
target="_blank" style="float:right; font-size:smaller">source</a>

### github_auth_device

``` python

def github_auth_device(
    wb:str='', n_polls:int=9999
):

```

*Authenticate with GitHub, polling up to `n_polls` times to wait for
completion*

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:

``` python
github_auth_device('n',n_polls=0)
```

    First copy your one-time code: 4ACE-3C18
    Then visit https://github.com/login/device in your browser, and paste the code when prompted.
    Waiting for authorization...Authentication not complete!
