---

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"
---
<!--

#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: 02_auth.ipynb
# command to build the docs after a change: nbdev_build_docs

-->

<div class="container" id="notebook-container">
        
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="Scopes">Scopes<a class="anchor-link" href="#Scopes"> </a></h2>
</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="scope_str" class="doc_header"><code>scope_str</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/auth.py#L25" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>scope_str</code>(<strong>*<code>scopes</code></strong>)</p>
</blockquote>
<p>Convert <code>scopes</code> into a comma-separated string</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">scope_str</span><span class="p">(</span><span class="n">Scope</span><span class="o">.</span><span class="n">repo</span><span class="p">,</span><span class="n">Scope</span><span class="o">.</span><span class="n">admin_public_key</span><span class="p">,</span><span class="n">Scope</span><span class="o">.</span><span class="n">public_repo</span><span class="p">)</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">



<div class="output_text output_subarea output_execute_result">
<pre>&#39;repo,admin:public_key,public_repo&#39;</pre>
</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h2 id="GhDeviceAuth" class="doc_header"><code>class</code> <code>GhDeviceAuth</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/auth.py#L33" class="source_link" style="float:right">[source]</a></h2><blockquote><p><code>GhDeviceAuth</code>(<strong><code>client_id</code></strong>=<em><code>'771f3c3af93face45f52'</code></em>, <strong>*<code>scopes</code></strong>) :: <code>GetAttrBase</code></p>
</blockquote>
<p>Get an oauth token using the GitHub API device flow</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Creating a <a href="/auth.html#GhDeviceAuth"><code>GhDeviceAuth</code></a> will complete the first step in the GitHub API device flow, getting device and user codes.</p>

</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">ghauth</span> <span class="o">=</span> <span class="n">GhDeviceAuth</span><span class="p">()</span>
<span class="n">ghauth</span><span class="o">.</span><span class="n">device_code</span><span class="p">,</span><span class="n">ghauth</span><span class="o">.</span><span class="n">user_code</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">



<div class="output_text output_subarea output_execute_result">
<pre>(&#39;4a862fd5d474b58cfad0e2432f47c33a2976facc&#39;, &#39;28F1-E3A6&#39;)</pre>
</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhDeviceAuth.url_docs" class="doc_header"><code>GhDeviceAuth.url_docs</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/auth.py#L44" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhDeviceAuth.url_docs</code>()</p>
</blockquote>
<p>Default instructions on how to authenticate</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>You can provide your own instructions on how to authenticate, or just print this out:</p>

</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">ghauth</span><span class="o">.</span><span class="n">url_docs</span><span class="p">())</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>First copy your one-time code: 28F1-E3A6
Then visit https://github.com/login/device in your browser, and paste the code when prompted.
</pre>
</div>
</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhDeviceAuth.open_browser" class="doc_header"><code>GhDeviceAuth.open_browser</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/auth.py#L51" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhDeviceAuth.open_browser</code>()</p>
</blockquote>
<p>Open a web browser with the verification URL</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>This uses Python's <code>webbrowser.open</code>, which will use the user's default web browser. This won't work well if the user is using a remote terminal.</p>

</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhDeviceAuth.auth" class="doc_header"><code>GhDeviceAuth.auth</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/auth.py#L57" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhDeviceAuth.auth</code>()</p>
</blockquote>
<p>Return token if authentication complete, or <code>None</code> otherwise</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Until the user has completed authentication in the browser, this will return None. Normally, you won't call this directly, but will call <code>wait</code> (see below), which will repeatedly call <a href="/auth.html"><code>auth</code></a> until authentication is complete.</p>

</div>
</div>
</div>
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">ghauth</span><span class="o">.</span><span class="n">auth</span><span class="p">())</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">

<div class="output_subarea output_stream output_stdout output_text">
<pre>None
</pre>
</div>
</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="GhDeviceAuth.wait" class="doc_header"><code>GhDeviceAuth.wait</code><a href="https://github.com/fastai/ghapi/tree/master/ghapi/auth.py#L70" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>GhDeviceAuth.wait</code>(<strong><code>cb</code></strong>:<code>callable</code>=<em><code>None</code></em>, <strong><code>n_polls</code></strong>=<em><code>9999</code></em>)</p>
</blockquote>
<p>Wait up to <code>n_polls</code> times for authentication to complete, calling <code>cb</code> after each poll, if passed</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>If you pass a callback to <code>cb</code>, it will be called after each unsuccessful check for user authentication. For instance, to print a <code>.</code> to the screen after each poll, and store the token in a variable <code>token</code> when complete, you could use:</p>
<div class="highlight"><pre><span></span><span class="n">token</span> <span class="o">=</span> <span class="n">ghauth</span><span class="o">.</span><span class="n">wait</span><span class="p">(</span><span class="k">lambda</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>
</div>
 

