DEV Community

Budy mnvenom
Budy mnvenom

Posted on

Firebase Send email verification using python

CHECK PREVIOUS TUTORIAL AND EXPLANATION Python and firebase auth
Now to send an email verification using python. You need to send a POST request to

https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode
Enter fullscreen mode Exit fullscreen mode

You need also the token that you get after ** sign in *or * sign up **.

import requests

apikey='..................'# the web api key

idToken='eyJh1pVjuYvfX72ewSqUxRNUNoGKgdhsaYdeOjs9OsQ......'#token

def VerifyEmail(idToken):
    headers = {
        'Content-Type': 'application/json',
    }
    data='{"requestType":"VERIFY_EMAIL","idToken":"'+idToken+'"}'
    r = requests.post('https://identitytoolkit.googleapis.com/v1/accounts:sendOobCode?key={}'.format(apikey), headers=headers, data=data)
    if 'error' in r.json().keys():
        return {'status':'error','message':r.json()['error']['message']}
    if 'email' in r.json().keys():
        return {'status':'success','email':r.json()['email']}
Enter fullscreen mode Exit fullscreen mode

run :

VerifyEmail(idToken)
Enter fullscreen mode Exit fullscreen mode

result:

Success

{'status': 'success', 'email': 'user@example.com'}
Enter fullscreen mode Exit fullscreen mode

The user will receive an email from you like this :

imageedit_4_5081572962.jpg
*Failed *

{'status': 'error', 'message': 'INVALID_ID_TOKEN'}
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
berbru profile image
ber-bru

Por que me da o erro de 'INVALID_ID_TOKEN'? Se informe corretamente o idToken