'jupyter/voila: iframe, how to provide token / cookie

I can open a web site in a cell with e.g.

from IPython.display import HTML
display(HTML('<iframe src=http://npr.org width=700 height=350></iframe>'))

or

from IPython.display import IFrame
display(IFrame('http://npr.org', width=700, height=350))

I am using this to display a small app/web service I made. However, my site requires authentication and will redirect to another url (keycloak) that will not allow me to embed (which is correct for security reasons...)

I can get the security token using requests independently, e.g.

import requests
headers = {
    'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
  'client_id': 'admin-cli',
  'username': 'myUserName',
  'password': 'myPassword',
  'grant_type': 'password'
}
response = requests.post('https://myKeycloakIP.foo.bar/auth/realms/myRealm/protocol/openid-connect/token', headers=headers, data=data)

However I do not know how to add the cookie to the iframe.... How can I add the cookie/access_token so that it does not have to redirect?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source