Authlib: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

530 views Asked by At

I'm using this blog post from Authlib's blog to add google oauth to a FastAPI app. I'm from Iran and Everything works fine when my VPN is on, but when it's off and I try to login to app with google with an Iranian IP, the Authlib can't parse id_token and it displays the following error:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Here is the code I use:

app = FastAPI()
app.add_middleware(SessionMiddleware, secret_key="secret-string")

config = Config('/usr/src/app/google-auth.env')
oauth = OAuth(config)
oauth.register(
    name='google',
    server_metadata_url='https://accounts.google.com/.well-known/openid-configuration',
    client_kwargs={
        'scope': 'openid email profile'
    }
)

@app.route('/login')
async def login(request: Request):
    redirect_uri = request.url_for('auth') # absolute url for callback
    return await oauth.google.authorize_redirect(request, redirect_uri)


@app.route('/auth')
async def auth(request: Request):
    token = await oauth.google.authorize_access_token(request)
    print(token)
    user = await oauth.google.parse_id_token(request, token)
    return RedirectResponse(url='/')

The error arises on line oauth.google.parse_id_token and I see the token printed in the command line. I even parsed the token manually here and it's fine and it has the user's data.

Why Authlib can't parse the token, when the user has an Iranian IP? Is it because of the sanctions that google doesn't allow Iranians to use most of the services? And how can I change the code to parse the token in another way?

0

There are 0 answers