Ask Your Question
0

How can I fix the TypeError that says it is impossible to convert the 'URL' object into bytes when implementing the AuthLib Google Auth example for FastAPI?

asked 2022-12-12 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-05-07 06:00:00 +0000

devzero gravatar image

The error is occurring because the URL object cannot be converted into bytes directly. To fix this error, you can use the str.encode() method to convert the URL object into bytes before passing it to the Authorization header.

Here's an example of how to modify the example code to fix the error:

import requests
from fastapi import FastAPI, Depends
from fastapi.responses import HTMLResponse
from authlib.integrations.requests_client import OAuth2Session

app = FastAPI()

# configure the oauth2 client
client_id = 'your_client_id_here'
client_secret = 'your_client_secret_here'
redirect_uri = 'http://localhost:8000/api/google/auth'
scope = 'email profile openid'
access_token_url = 'https://accounts.google.com/o/oauth2/token'
access_token_params = None
authorize_url = 'https://accounts.google.com/o/oauth2/auth'
authorize_params = None
api_base_url = 'https://www.googleapis.com/oauth2/v1/'
client = OAuth2Session(client_id, client_secret, redirect_uri=redirect_uri, scope=scope)
client.register('google', access_token_url=access_token_url, access_token_params=access_token_params,
                authorize_url=authorize_url, authorize_params=authorize_params, api_base_url=api_base_url)

# define a dependency to get the current user
def get_current_user(token: str = Depends(client.oauth2_token)) -> dict:
    url = 'https://www.googleapis.com/oauth2/v2/userinfo'
    headers = {'Authorization': f'Bearer {token.get("access_token")}'.encode()}
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response.json()

# define the login route
@app.get('/login')
async def login() -> HTMLResponse:
    authorization_url, state = client.create_authorization_url('google')
    return HTMLResponse(f'<a href="{authorization_url}">Login with Google</a>')

# define the callback route
@app.get('/api/google/auth')
async def auth(code: str):
    token = client.fetch_token('google', authorization_response=code)
    return {'access_token': token.get('access_token'), 'token_type': token.get('token_type')}

# define a protected route
@app.get('/protected')
async def protected(current_user: dict = Depends(get_current_user)):
    return {'current_user': current_user}

In the getcurrentuser function, you can modify the Authorization header to encode the token as bytes before passing it to the headers dictionary. This should fix the TypeError and allow the code to run without issues.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-12 11:00:00 +0000

Seen: 10 times

Last updated: May 07 '22