How to connect to HBP Rest API using Jupyter Notebook?


#1

I would like to access the Software Catalog API in Jupyter Notebook. At the moment, we don’t have a dedicated Python client for it so I would like to use standard Python requests.

In order to do so, I need to be able to retrieve my current OIDC token. Is there a way to do that?


#2

Since next release (beginning of March) it will be possible to get token like this:
get_bbp_client().get_access_token()


#3

In the current release, it would seem that you can get the same information from:
get_bbp_client().task.oauth_client.credentials.access_token

Will this also work within a task?


#4

better way in the current release is
get_bbp_client().task.oauth_client.get_auth_header()

in the task it will not work unfortunately
that’s the interface provided to the task
https://developer.humanbrainproject.eu/docs/projects/task-sdk/0.0.28/api.html#uri-operations-interface
why do you need token inside task?


#5

We are trying to set up a task so that we can submit a job to the Neuromorphic platform API and / or the HPC platform. This will then put the results in the document service (I believe that this is accessible from the task API). To do either of these things requires an OIDC access token.


#6

Ok, I see now.
Yes, document service API is accessible through interface I posted earlier.
We will have to extend the interface to provide method to retrieve token.
I can provide you workaround for now in case it is urgent.


#7

Yes, a workaround would be good so that I can try out the rest of the operation.

Thanks for the fast responses!


#8

Sorry, unfortunately I was wrong and there is no simple workaround. I am working on this topic now and it should be solved in the coming days. I will inform you with separate message.

Yury


#9

New api has been added to UriOps
https://developer.humanbrainproject.eu/docs/projects/task-sdk/0.0.29/api.html#active_worker.TaskRPC.IUriOps.get_oauth_token

You can use it like

token = <your_task>.task.uri.get_oauth_token()

Remember that tokens are expiring in prod in about 30 minutes. So if your task can run longer you have to request token again at some point.

Yury


#10

Example task which returns token
and tries to send GET request to url with the token so you can test if your API is accessible from the task service
https://collab.humanbrainproject.eu/#/collab/472/nav/4929


#11

Hi Yuri,
I am currently using our own Jupyter notebook installation with OAuth2 module installed, I need to get the OAuth2 token form the nb console as I authenticate, but anyway I have some problem with token expiration.

My question is: how did you implement the function:

get_bbp_client().task.oauth_client.get_auth_header()

I try to install it but it’s not found

!pip3 install --upgrade pyunicore
!pip3 install --upgrade “hbp-service-client==1.1.0”

8 hdr = get_bbp_client().task.oauth_client.get_auth_header()

NameError: name ‘get_bbp_client’ is not defined

So how can I do to get the OAuth2 token, did you implement some function somewhere in the collab? how is this implemented?

thanks in advance for any help

Cheers,
Michele


#12

Dear Michele,

The function you are looking for is not in the hbp-service-client, but in the now deprecated bbp-service-client. That package is still present in the Notebooks in the Collaboratory for compatibility reasons.

The method in question was doing one of the OIDC Authentication flows, which you can also do yourself. Some documentation:

https://collab.humanbrainproject.eu/#/collab/54/nav/35256

Hope this helps.

Kind regards,
Akos


#13

Thanks for the answer,
anyway my problem remains how to pass the token to the Jupyter collab after first authentication on JuyterHub.
The first token expires as soon as the expiration time has been passed, the problem arise mostly because there’s no way to pass the OAuth2 new token from the JupyterHub server authentication method to the Jupyter console (this can be don only once after the spanwer process has been started the first time, pre_spawner_start() method).

In order to obtain a brand new token I should force the user to authenticate again i.e. inside the Jupyter console, after authenticating first with the Jupyter OAuth2 module.

Since the first token expire I cannot refresh anything and I cannot ask the user to authenticate twice inside the Jupyter.

So again, how is possible from the HBP collaboratory that the OAuth2 first new token can be passed through the hbp_client method to the Jupyter notebook console?

Sorry if not having being clear, I am also a little bit puzzled :wink:

Cheers


#14

anyway my problem remains how to pass the token to the Jupyter collab after first authentication on JuyterHub.

I am confused. You said you are using your own Jupyter installation, but you say "Jupyter collab’. Is the problem related to the JupyterHub service of the Collaboratory?

So again, how is possible from the HBP collaboratory that the OAuth2
first new token can be passed through the hbp_client method to the
Jupyter notebook console?

I am not sure what do you mean. The hbp_client library does not print tokens, it rather takes one during instantiation. It is the deprecated bbp_client that does so, and I am unfamiliar with the implementation.

In general if you need to obtain new access tokens after their expiration you need a refresh token. I believe the bbp_client was also using a mechanism to exchange a refresh token to new access tokens.

Kind regards,
Akos