title: Jupyter notebook API to get notebooks
body: <p>I have my jupyter hub and single-user server running. I am using Jupyter notebook REST APIs to get all available notebooks. I am using <a href="http://127.0.0.1:8000/user/username/api/contents" rel="nofollow noreferrer">http://127.0.0.1:8000/user/username/api/contents</a> . With this I am able to see all available notebooks on mu UI. </p>
  
  <p>However when I try to do the same from my python script I am getting 200 response code but unable to extract any content as its redirecting to login page where I need to enter username and password for my user. I also tried to pass these credentials through requests package. Something like this</p>
  
  <pre><code>import requests
  api_url =  'http://127.0.0.1:8000/user/username/api/contents'
  result = requests.get(api_url, auth=('username', 'password'))
  print result
  print result.json()
  </code></pre>
  
  <p>The outpus is:</p>
  
  <pre><code>&lt;!DOCTYPE HTML&gt;
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;meta charset="utf-8"&gt;
  
      &lt;title&gt;Jupyter Hub&lt;/title&gt;
      &lt;meta http-equiv="X-UA-Compatible" content="chrome=1"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
  
  
      &lt;link rel="stylesheet" href="/hub/static/css/style.min.css?v=d96e0760e0c2b7356ce89635b646c350" type="text/css"/&gt;
  
      &lt;script src="/hub/static/components/requirejs/require.js?v=6da8be361b9ee26c5e721e76c6d4afce" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
      &lt;script&gt;
        require.config({
  
            urlArgs: "v=(&amp;#39;20170907062813&amp;#39;,)",
  
            baseUrl: '/hub/static/js',
            paths: {
              components: '../components',
              jquery: '../components/jquery/jquery.min',
              bootstrap: '../components/bootstrap/js/bootstrap.min',
              moment: "../components/moment/moment",
            },
            shim: {
              bootstrap: {
                deps: ["jquery"],
                exports: "bootstrap"
              },
            }
        });
      &lt;/script&gt;
  
      &lt;script type="text/javascript"&gt;
        window.jhdata = {
          base_url: "/hub/",
          prefix: "/",
  
        }
      &lt;/script&gt;
  
  
  
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;noscript&gt;
    &lt;div id='noscript'&gt;
      Jupyter Hub requires JavaScript.&lt;br&gt;
      Please enable it to proceed.
    &lt;/div&gt;
  &lt;/noscript&gt;
  
  &lt;div id="header" class="navbar navbar-static-top"&gt;
    &lt;div class="container"&gt;
    &lt;span id="jupyterhub-logo" class="pull-left"&gt;&lt;a href="/hub/"&gt;&lt;img src='/hub/logo' alt='JupyterHub' class='jpy-logo' title='Home'/&gt;&lt;/a&gt;&lt;/span&gt;
  
    &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;div id="login-main" class="container"&gt;
  
  &lt;form action="/hub/login?next=%2Fhub%2Fuser%2Fpnda%2Fapi%2Fcontents" method="post" role="form"&gt;
    &lt;div class="auth-form-header"&gt;
      Sign in
    &lt;/div&gt;
    &lt;div class='auth-form-body'&gt;
  
      &lt;p id='insecure-login-warning' class='hidden'&gt;
      Warning: JupyterHub seems to be served over an unsecured HTTP connection.
      We strongly recommend enabling HTTPS for JupyterHub.
      &lt;/p&gt;
  
  
      &lt;label for="username_input"&gt;Username:&lt;/label&gt;
      &lt;input
        id="username_input"
        type="username"
        autocapitalize="off"
        autocorrect="off"
        class="form-control"
        name="username"
        val=""
        tabindex="1"
        autofocus="autofocus"
      /&gt;
      &lt;label for='password_input'&gt;Password:&lt;/label&gt;
      &lt;input
        type="password"
        class="form-control"
        name="password"
        id="password_input"
        tabindex="2"
      /&gt;
  
      &lt;input
        type="submit"
        id="login_submit"
        class='btn btn-jupyter'
        value='Sign In'
        tabindex="3"
      /&gt;
    &lt;/div&gt;
  &lt;/form&gt;
  
  &lt;/div&gt;
  
  
  &lt;div class="modal fade" id="error-dialog" tabindex="-1" role="dialog" aria-labelledby="error-label" aria-hidden="true"&gt;
    &lt;div class="modal-dialog"&gt;
      &lt;div class="modal-content"&gt;
        &lt;div class="modal-header"&gt;
          &lt;button type="button" class="close" data-dismiss="modal"&gt;&lt;span aria-hidden="true"&gt;&amp;times;&lt;/span&gt;&lt;span class="sr-only"&gt;Close&lt;/span&gt;&lt;/button&gt;
          &lt;h4 class="modal-title" id="error-label"&gt;Error&lt;/h4&gt;
        &lt;/div&gt;
        &lt;div class="modal-body"&gt;
  
    &lt;div class="ajax-error"&gt;
      The error
    &lt;/div&gt;
  
        &lt;/div&gt;
        &lt;div class="modal-footer"&gt;
          &lt;button type="button" class="btn btn-default" data-dismiss="modal"&gt;Cancel&lt;/button&gt;
          &lt;button type="button" class="btn btn-primary" data-dismiss="modal" data-dismiss="modal"&gt;OK&lt;/button&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  
  
  
  
  &lt;script&gt;
  if (window.location.protocol === "http:") {
    // unhide http warning
    var warning = document.getElementById('insecure-login-warning');
    warning.className = warning.className.replace(/\bhidden\b/, '');
  }
  &lt;/script&gt;
  
  
  
  &lt;/body&gt;
  
  &lt;/html&gt;
  
  Traceback (most recent call last):
    File "test.py", line 31, in &lt;module&gt;
      check_running()
    File "test.py", line 28, in check_running
      contents = r.json()
    File "/usr/lib/python2.7/dist-packages/requests/models.py", line 740, in json
      return json.loads(self.content.decode(encoding), **kwargs)
    File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
      return _default_decoder.decode(s)
    File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
      obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
      raise ValueError("No JSON object could be decoded")
  </code></pre>
  
  <p>I am fairly new to Jupyter. My I missing out on something. How else can I authenticate my user prior calling any API.</p>
  
  <p>Saw this in jupyerhub documentation - Note that the API token authorizes JupyterHub REST API requests. The same token does not authorize access to the Jupyter Notebook REST API provided by notebook servers managed by JupyterHub. A different token is used to access the Jupyter Notebook API.</p>
  
  <p>So this is the exactly what I am looking for. How can I create this different token so as to access jupyter notebook APIs thorugh hub.</p>