I have made a flask/python website which can create and edit events on a user's google calendar. Obviously the user has to give their permission via OAuth2 (which I struggled to understand but managed to make work in the end). Currently I am forcing the user to register and login to my site and I store various user settings and the refresh token attached to whatever username they select when they register with my site.... But now I want the user to be able to allow the user the use my site without having to register. I still want to store user settings and a refresh token but now I need to have some sort of label to identify the user so I know it's them when they come back (perhaps they will access my site from a different PC). Is there some string that I will have access to as part of the OAuth2 process that would serve to uniquely identify the user or do I need to do some additional step to grab such a string...
EDIT: looking at the comment made by shox and looking at the most upvoted answer to this SO question it seems that at the end of the oauth process I should...
send a GET to https://www.googleapis.com/oauth2/v3/userinfo, using the OAuth2 bearer token you just received, and you will get a response with some information about the user (id, name, etc.).
Unfortunately I'm not quite sure what that means nor how to code it in python. My best guess was as follows:
r = requests.get( "https://www.googleapis.com/oauth2/v3/userinfo",
params = {'token': credentials.token } )
data = r.json()
But data turns out to be {'error': 'invalid_request', 'error_description': 'Invalid Credentials'}
SOLVED: ... seems I needed to use the string "access_token" instead of "token" and now data contains an email address and a few other bits and bobs.
r = requests.get( "https://www.googleapis.com/oauth2/v3/userinfo",
params = {'access_token': credentials.token } )
data = r.json()