@blueprint.route('/redirect')
def oauth_redirect_callback():
  # Have to do authentication!
  rest.default_user_authentication()

  auth_code = flask.request.args.get('code', None)
  state = flask.request.args.get('state', None)
  if auth_code is None or state is None:
    logging.info('No auth_code of state found!')
    flask.abort(400)

  logging.info('Got OAuth callback state="%s", auth_code="%s"',
               state, auth_code)

  account = Account.get_by_id(state)
  if account is None:
    logging.info('Account \'%s\' not found!', state)
    flask.abort(400)

  account.auth_code = auth_code
  account.refresh_access_token()
  account.refresh_devices()
  account.put()

  return "OK"
Step 2: Nest (etc) server redirects back to us
dataset/ETHPy150Open tomwilkie/awesomation/src/appengine/account.py/oauth_redirect_callback