flaskgoogle-apirauth

How do I obtain user info (username / id) from Google API using Flask and rauth?


Here is what I have got so far.

GOOGLE_AUTH_URI = 'https://accounts.google.com/o/oauth2/auth'
GOOGLE_REVOKE_URI = 'https://accounts.google.com/o/oauth2/revoke'
GOOGLE_TOKEN_URI = 'https://accounts.google.com/o/oauth2/token'
GOOGLE_BASE_URL = 'https://www.googleapis.com/plus/v1/'

redirect_uri = '127.0.0.1:5000/callback'    

google = OAuth2Service(
name='google',
client_id = GOOGLE_APP_ID,
client_secret = GOOGLE_APP_SECRET,
access_token_url=GOOGLE_TOKEN_URI,
authorize_url=GOOGLE_AUTH_URI,
base_url = GOOGLE_BASE_URL)


@app.route('/login/google')
def googleLogin():
params = {'scope': 'https://www.googleapis.com/auth/userinfo.profile',
          'access_type': 'offline',
          'response_type': 'code',
          'redirect_uri': redirect_uri}
return redirect(google.get_authorize_url(**params))

@app.route('/callback')
def callback():
    credentials = google.get_access_token(data = {'code':request.args['code'],
        'grant_type': 'authorization_code',
        'redirect_uri': redirect_uri},
        decoder = json.loads)

    return jsonify(refresh_token=credentials)

Now I want to store the user information after the user gives his/her consent in my database. How should I go about doing it?


Solution

  • Figured out the problem. The get_access_token function is flawed. Follow this link for more information