In [1]:
#some imports
from oauth2client import client
import httplib2
import ipywidgets as widgets
You need to upload first a client_secrets.json of your credentials (Oauth client ID of type other).
You may want to adjust the scope parameter to yours needs.
In [2]:
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope=['https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/compute'],
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
In [3]:
#the authroization url
auth_uri = flow.step1_get_authorize_url()
#the widget that contains the link
authorizationLink=widgets.HTML(
value="<p>Follow the link to authorize this notebook to connect to compute on your google cloud.</p>"+
"<p>And copy paste the obtained authorization code in the input below.</p>"+
"<a target=_blank href="+auth_uri+">"+auth_uri+"</a>")
#the widget to insert the autorization code
authorizationCode=widgets.Text(description="Your authorization code:")
display(authorizationLink)
display(authorizationCode)
def handle_submit(sender):
#when the autorization code is entered, close the widgets and go on
authorizationCode.close()
authorizationLink.close()
display(widgets.HTML("Go on executing the next cells."))
authorizationCode.on_submit(handle_submit)
In [4]:
#create the credentials with the autorization code
credentials = flow.step2_exchange(authorizationCode.value)
In [5]:
# a button to confirm the saving of the credentials to disk
button=widgets.ToggleButton(value=False,
description='Save the credentials?',
disabled=False,
button_style='info' ,#, 'info', 'warning', 'danger' or ''
tooltip='On disk',
icon='save'
)
display(button)
def on_click(sender):
#when the buttonis click, we save the credentials to disk
from oauth2client.file import Storage
storage = Storage("oauth2.dat")
storage.put(credentials)
button.close()
display(widgets.Valid(
value=True,
description='Saved!',
disabled=False))
button.observe(on_click, 'value')
In [ ]: