# OAuth See the [docs page](https://www.fastht.ml/docs/explains/oauth.html) for an explanation of how to use this. ``` python from IPython.display import Markdown ``` ------------------------------------------------------------------------ source ### GoogleAppClient > GoogleAppClient (client_id, client_secret, code=None, scope=None, > project_id=None, **kwargs) *A `WebApplicationClient` for Google oauth2* ------------------------------------------------------------------------ source ### GitHubAppClient > GitHubAppClient (client_id, client_secret, code=None, scope=None, > **kwargs) *A `WebApplicationClient` for GitHub oauth2* ------------------------------------------------------------------------ source ### HuggingFaceClient > HuggingFaceClient (client_id, client_secret, code=None, scope=None, > state=None, **kwargs) *A `WebApplicationClient` for HuggingFace oauth2* ------------------------------------------------------------------------ source ### DiscordAppClient > DiscordAppClient (client_id, client_secret, is_user=False, perms=0, > scope=None, **kwargs) *A `WebApplicationClient` for Discord oauth2* ------------------------------------------------------------------------ source ### Auth0AppClient > Auth0AppClient (domain, client_id, client_secret, code=None, scope=None, > redirect_uri='', **kwargs) *A `WebApplicationClient` for Auth0 OAuth2* ``` python # cli = GoogleAppClient.from_file('client_secret.json') ``` ------------------------------------------------------------------------ source ### WebApplicationClient.login_link > WebApplicationClient.login_link (redirect_uri, scope=None, state=None, > **kwargs) *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. ``` python from fasthtml.jupyter import * ``` ``` python redir_path = '/redirect' port = 8000 ``` ``` python app,rt = fast_app() server = JupyUvi(app, port=port) ``` ------------------------------------------------------------------------ source ### redir_url > redir_url (request, redir_path, scheme=None) *Get the redir url for the host in `request`* ``` python @rt def index(request): redir = redir_url(request, redir_path) return A('login', href=cli.login_link(redir), target='_blank') ``` ------------------------------------------------------------------------ source ### \_AppClient.parse_response > _AppClient.parse_response (code, redirect_uri) *Get the token from the oauth2 server response* ------------------------------------------------------------------------ source ### \_AppClient.get_info > _AppClient.get_info (token=None) *Get the info for authenticated user* ------------------------------------------------------------------------ source ### \_AppClient.retr_info > _AppClient.retr_info (code, redirect_uri) *Combines `parse_response` and `get_info`* ``` python @rt(redir_path) def get(request, code:str): redir = redir_url(request, redir_path) info = cli.retr_info(code, redir) return P(f'Login successful for {info["name"]}!') ``` ``` python # HTMX() ``` ``` python server.stop() ``` ------------------------------------------------------------------------ source ### \_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](https://www.fastht.ml/docs/explains/oauth.html) 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"]`. ------------------------------------------------------------------------ source ### url_match > url_match (url, patterns=('^(localhost|127\\.0\\.0\\.1)(:\\d+)?$',)) ------------------------------------------------------------------------ source ### OAuth > OAuth (app, cli, skip=None, redir_path='/redirect', error_path='/error', > logout_path='/logout', login_path='/login', https=True, > http_patterns=('^(localhost|127\\.0\\.0\\.1)(:\\d+)?$',)) *Initialize self. See help(type(self)) for accurate signature.* ### Google helpers ------------------------------------------------------------------------ source ### GoogleAppClient.consent_url > GoogleAppClient.consent_url (proj=None) *Get Google OAuth consent screen URL* ------------------------------------------------------------------------ source ### GoogleAppClient.consent_url > GoogleAppClient.consent_url (proj=None) *Get Google OAuth consent screen URL* ------------------------------------------------------------------------ source ### Credentials.update > Credentials.update () *Refresh the credentials if they are expired, and return them* ------------------------------------------------------------------------ source ### Credentials.update > Credentials.update () *Refresh the credentials if they are expired, and return them* ------------------------------------------------------------------------ source ### Credentials.save > Credentials.save (fname) *Save credentials to `fname`* ------------------------------------------------------------------------ source ### Credentials.save > Credentials.save (fname) *Save credentials to `fname`* ------------------------------------------------------------------------ source ### load_creds > load_creds (fname) *Load credentials from `fname`* ------------------------------------------------------------------------ source ### GoogleAppClient.creds > GoogleAppClient.creds () *Create `Credentials` from the client, refreshing if needed* ------------------------------------------------------------------------ source ### GoogleAppClient.creds > GoogleAppClient.creds () *Create `Credentials` from the client, refreshing if needed*