Reputation: 51
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 http://127.0.0.1:8000/user/username/api/contents . With this I am able to see all available notebooks on mu UI.
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
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()
The outpus is:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Jupyter Hub</title>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="/hub/static/css/style.min.css?v=d96e0760e0c2b7356ce89635b646c350" type="text/css"/>
<script src="/hub/static/components/requirejs/require.js?v=6da8be361b9ee26c5e721e76c6d4afce" type="text/javascript" charset="utf-8"></script>
<script>
require.config({
urlArgs: "v=('20170907062813',)",
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"
},
}
});
</script>
<script type="text/javascript">
window.jhdata = {
base_url: "/hub/",
prefix: "/",
}
</script>
</head>
<body>
<noscript>
<div id='noscript'>
Jupyter Hub requires JavaScript.<br>
Please enable it to proceed.
</div>
</noscript>
<div id="header" class="navbar navbar-static-top">
<div class="container">
<span id="jupyterhub-logo" class="pull-left"><a href="/hub/"><img src='/hub/logo' alt='JupyterHub' class='jpy-logo' title='Home'/></a></span>
</div>
</div>
<div id="login-main" class="container">
<form action="/hub/login?next=%2Fhub%2Fuser%2Fpnda%2Fapi%2Fcontents" method="post" role="form">
<div class="auth-form-header">
Sign in
</div>
<div class='auth-form-body'>
<p id='insecure-login-warning' class='hidden'>
Warning: JupyterHub seems to be served over an unsecured HTTP connection.
We strongly recommend enabling HTTPS for JupyterHub.
</p>
<label for="username_input">Username:</label>
<input
id="username_input"
type="username"
autocapitalize="off"
autocorrect="off"
class="form-control"
name="username"
val=""
tabindex="1"
autofocus="autofocus"
/>
<label for='password_input'>Password:</label>
<input
type="password"
class="form-control"
name="password"
id="password_input"
tabindex="2"
/>
<input
type="submit"
id="login_submit"
class='btn btn-jupyter'
value='Sign In'
tabindex="3"
/>
</div>
</form>
</div>
<div class="modal fade" id="error-dialog" tabindex="-1" role="dialog" aria-labelledby="error-label" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="error-label">Error</h4>
</div>
<div class="modal-body">
<div class="ajax-error">
The error
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" data-dismiss="modal" data-dismiss="modal">OK</button>
</div>
</div>
</div>
</div>
<script>
if (window.location.protocol === "http:") {
// unhide http warning
var warning = document.getElementById('insecure-login-warning');
warning.className = warning.className.replace(/\bhidden\b/, '');
}
</script>
</body>
</html>
Traceback (most recent call last):
File "test.py", line 31, in <module>
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")
I am fairly new to Jupyter. My I missing out on something. How else can I authenticate my user prior calling any API.
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.
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.
Upvotes: 2
Views: 5756
Reputation: 806
Jupyter Notebooks require an access token (this should show when you start up jupyter notebook). In my case, something like this, then worked:
import requests
#Note that the url doesn't have a user or username attribute
api_url = 'http://127.0.0.1:8888/api/contents'
payload = {'token': 'xxxxxx'}
result = requests.get(api_url
#, auth=('username', 'password')
,params = payload)
Upvotes: 4