【问题标题】:OAuthException: Invalid response from googleOAuthException:来自谷歌的无效响应
【发布时间】:2019-04-20 16:24:08
【问题描述】:

所以我正在从谷歌云 shell 运行我的 Flask 应用程序。在这个应用程序中,用户需要使用他们的谷歌帐户登录。我使用云外壳安装了所有必需的库。

当我在云 shell 中运行应用程序时,在选择了我要登录应用程序的 google 帐户后,出现此错误

flask_oauth.OAuthException
OAuthException: Invalid response from google

如果我从本地主机运行,一切正常。

非常感谢任何形式的帮助。

PS:这是代码

Flask.py:

import logging
from flask import Flask, render_template, redirect, url_for, session, make_response
from flask_oauth import OAuth
from urllib2 import Request, urlopen, URLError
import MySQLdb
import os
import json

Client_Id = my client id
Client_Secret = my client secret
Redirect_URI = '/callback'

SECRET_KEY = 'funny cat'
DEBUG = True

app = Flask(__name__)
app.debug = DEBUG
app.secret_key = SECRET_KEY
oauth = OAuth()

google = oauth.remote_app('google', base_url='https://www.google.com/accounts/', authorize_url='https://accounts.google.com/o/oauth2/auth', request_token_url=None, request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.profile', 'response_type': 'code'}, access_token_url='https://accounts.google.com/o/oauth2/token', access_token_method='POST', access_token_params={'grant_type': 'authorization_code'}, consumer_key=Client_Id, consumer_secret=Client_Secret)

@app.route('/')
def index():
    return render_template("webpage1.html")                  

@app.route('/login',methods=['post','get'])
def login():
    access_token = session.get('access_token')
    if access_token is None:
        return redirect(url_for('direct'))

    access_token = access_token[0]
    headers = {'Authorization': 'OAuth '+access_token}
    req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
                  None, headers)
    try:
        res = urlopen(req)
    except URLError, e:
        if e.code == 401:
            session.pop('access_token', None)
            return redirect(url_for('direct'))
        return res.read()
    data = json.load(res)
    return render_template("webpage2.html", data = data)

@app.route('/direct')
def direct():
    callback=url_for('authorized', _external=True)
    return google.authorize(callback=callback)

@app.route(Redirect_URI)
@google.authorized_handler
def authorized(resp):
    access_token = resp['access_token']
    session['access_token'] = access_token, ''
    return redirect(url_for('login'))

@app.route('/logout')
def logout():
    session.pop('access_token', None)
    return redirect(url_for('index'))

@google.tokengetter
def get_access_token():
    return session.get('access_token')

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)

我在 API 中的凭据是

Authorized JavaScript origins : https://5000-dot-4778310-dot-devshell.appspot.com   
Authorized redirect URIs : https://5000-dot-4778310-dot-devshell.appspot.com/callback   

为什么相同的client id和client secret的项目名称不同?

【问题讨论】:

  • 您在 Google Cloud Console 中的 Client ID for Web application 中进行了哪些配置? Authorized JavaScript originsAuthorized redirect URIs 必须正确。这意味着 URL 和端口号。
  • 这些没问题。我被要求选择我想用来登录的谷歌帐户。我也给了密码。问题似乎发生在回调之后。如果 Authorized JavaScript originsAuthorized redirect URIs 是问题所在,我一开始就不会被定向到 google 帐户页面。
  • 您的重定向网址不正确。正如我所提到的,您必须拥有正确的端口。您的 Flask 服务器在端口 8080 上配置为 HTTP。您的重定向 URI 配置为 HTTPS 和端口 443。它们不匹配。您的 Flask 服务器未运行 HTTPS,除了 localhost 模式外,这是强制性的。您的 Flask 服务器正在侦听 localhost,这意味着它永远不会收到回调。但是,如果您实际上是在 localhost 模式下运行它,那么您的重定向 URI 应该是 localhost:8080
  • 如果您在 localhost 模式下运行,那么您的 Web 浏览器也必须在同一台机器 (vm) 上运行。 Cloud Shell 没有网络浏览器。
  • 我应该在哪里进行更改以运行我的烧瓶服务器 HTTPS?

标签: python oauth-2.0 google-cloud-platform google-oauth google-cloud-shell


【解决方案1】:

这个问题实际上是如何使用在 Google Cloud Shell VM 中运行的 Flask / Python 设置 Google OAuth 2.0。

Google OAuth 2.0 授权有两种模式:localhost 模式和回调模式。

本地主机模式

这要求整个身份验证过程在具有本地运行的 Web 服务器的同一台机器上执行。连接到 Google Domain 的网络浏览器也必须在同一台机器上运行。浏览器不能在不同的机器(或 VM)上运行。由于 Cloud Shell 没有网络浏览器,因此无法使用此模式。

回调模式

这要求 Web 服务器运行时配置了 TLS。 Google OAuth 2.0 仅支持到 HTTPS 端点的回调 url。这还需要经过验证的域名和 SSL 证书。由于您既无法控制 Cloud Shell 域名,也没有证书的私钥,因此无法设置 TLS。

因此,这个问题的答案是无法设置在 Cloud Shell 中运行的服务器进程来处理 Google OAuth 2.0 身份验证。

在此用户的情况下,回调 URL 与他的 Flask Web 服务器的配置方式不匹配。在这种情况下,无法正确配置回调 URL。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-10
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 2021-06-06
    • 2022-11-10
    • 2013-02-20
    • 2011-04-04
    相关资源
    最近更新 更多