sael17
sael17

Reputation: 19

How to decode a JWT returned by the Google API to in Python

I am currently trying to access the information in the payload given by the JWT returned by the API. I have tried examples from the official jwt documentation but nothing seems to be working. My goal is to extract the name and the email from the document in order for me to use it in the web page, see Sign In With Google JavaScript API reference

I am getting the JWT returned by the API, but I am stuck in how to decode it for me to use.

This is all the code I have for this part

@app.route("/")
@app.route("/index",methods=["GET","POST"])
def index():
    if request.method == "POST":
        if request.form["credential"]:
            pass

where request.form["credential"] is a JWT in the form

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

How could be done with python? I am new at this so I am learning on the way :)

Upvotes: 1

Views: 1621

Answers (1)

PCDSandwichMan
PCDSandwichMan

Reputation: 2120

Give this a shot:

import jwt

def decode_jwt(token):
    try:
        decoded = jwt.decode(token, 'secret', algorithms=['HS256'])
        return decoded
    except:
        return None

Upvotes: 0

Related Questions