# FastAPI OAuth Client

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. It is build on top of Starlette, that means most of the code looks similar with Starlette code. You should first read documentation of:

1. Web OAuth Clients

2. Starlette OAuth Client

Here is how you would create a FastAPI application:

    from fastapi import FastAPI
    from starlette.middleware.sessions import SessionMiddleware

    app = FastAPI()
    # we need this to save temporary code & state in session
    app.add_middleware(SessionMiddleware, secret_key="some-random-string")

Since Authlib starlette requires using `request` instance, we need to expose that `request` to Authlib. According to the documentation on Using the Request Directly:

    from starlette.requests import Request

    @app.get("/login")
    def login_via_google(request: Request):
        redirect_uri = 'https://example.com/auth'
        return await oauth.google.authorize_redirect(request, redirect_uri)

    @app.get("/auth")
    def auth_via_google(request: Request):
        token = await oauth.google.authorize_access_token(request)
        user = await oauth.google.parse_id_token(request, token)
        return dict(user)

All other APIs are the same with Starlette.

## FastAPI OAuth 1.0 Client

We have a blog post about how to create Twitter login in FastAPI:

https://blog.authlib.org/2020/fastapi-twitter-login

## FastAPI OAuth 2.0 Client

We have a blog post about how to create Google login in FastAPI:

https://blog.authlib.org/2020/fastapi-google-login