from IPython.display import Markdown
OAuth
This is not yet thoroughly tested. See the docs page for an explanation of how to use this.
GoogleAppClient
GoogleAppClient (client_id, client_secret, code=None, scope=None, **kwargs)
A WebApplicationClient
for Google oauth2
GitHubAppClient
GitHubAppClient (client_id, client_secret, code=None, scope=None, **kwargs)
A WebApplicationClient
for GitHub oauth2
HuggingFaceClient
HuggingFaceClient (client_id, client_secret, code=None, scope=None, state=None, **kwargs)
A WebApplicationClient
for HuggingFace oauth2
DiscordAppClient
DiscordAppClient (client_id, client_secret, is_user=False, perms=0, scope=None, **kwargs)
A WebApplicationClient
for Discord oauth2
= GoogleAppClient.from_file('/Users/jhoward/git/nbs/oauth-test/client_secret.json') cli
WebApplicationClient.login_link
WebApplicationClient.login_link (redirect_uri, scope=None, state=None)
Get a login link for this client
Generating a login link that sends the user to the OAuth provider is done with client.login_link()
.
It can sometimes be useful to pass state to the OAuth provider, so that when the user returns you can pick up where they left off. This can be done by passing the state
parameter.
='http://localhost:8000/redirect'
redir cli.login_link(redir)
'https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id=457681028261-5i71skrhb7ko4l8mlug5i0230q980do7.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fredirect&scope=openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile'
def login_md(cli, redirect_uri, scope=None, state=None):
"Display login link in notebook (for testing)"
return Markdown(f'[login]({cli.login_link(redirect_uri, scope, state=state)})')
='test_state') login_md(cli, redir, state
_AppClient.parse_response
_AppClient.parse_response (code, redirect_uri)
Get the token from the oauth2 server response
decode
decode (code_url)
= 'http://localhost:8000/redirect?state=test_state&code=4%2F0AQlEd8xCOSfc7yjmmylO6BTVgWtAmji4GkfITsWecq0CXlm-8wBRgwNmkDmXQEdOqw0REQ&scope=email+profile+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&authuser=0&hd=answer.ai&prompt=consent'
code_url
= decode(code_url) code,state,redir
cli.parse_response(code, redir)print(state)
test_state
_AppClient.get_info
_AppClient.get_info (token=None)
Get the info for authenticated user
info
{'sub': '100000802623412015452',
'name': 'Jeremy Howard',
'given_name': 'Jeremy',
'family_name': 'Howard',
'picture': 'https://lh3.googleusercontent.com/a/ACg8ocID3bYiwh1wJNVjvlSUy0dGxvXbNjDt1hdhypQDinDf28DfEA=s96-c',
'email': '[email protected]',
'email_verified': True,
'hd': 'answer.ai'}
_AppClient.retr_info
_AppClient.retr_info (code, redirect_uri)
Combines parse_response
and get_info
_AppClient.retr_id
_AppClient.retr_id (code, redirect_uri)
Call retr_info
and then return id/subscriber value
After logging in via the provider, the user will be redirected back to the supplied redirect URL. The request to this URL will contain a code
parameter, which is used to get an access token and fetch the user’s profile information. See the explanation here for a worked example. You can either:
- Use client.retr_info(code) to get all the profile information, or
- Use client.retr_id(code) to get just the user’s ID.
After either of these calls, you can also access the access token (used to revoke access, for example) with client.token["access_token"]
.
OAuth
OAuth (app, cli, skip=None, redir_path='/redirect', logout_path='/logout', login_path='/login')
Initialize self. See help(type(self)) for accurate signature.