烧瓶会话不允许从 urlfetch 内容进行分配 - 内部服务器错误



我有一个在Google App Engine上运行的Python应用程序。我正在尝试在不使用 oauth 库的情况下执行 Oauth 2 身份验证。

我需要一个会话来存储从 Google 身份验证代码(访问令牌)返回的信息,并将其发送到下一个请求。当我尝试将 JSON 存储到烧瓶会话密钥中时,我的应用进入内部服务器错误,没有调试信息。我的代码实际上是谷歌的HTTP Rest示例的复制粘贴,该示例位于其Oauth2 for Web服务器应用程序文档中(链接在代码中注释)。

import logging
import flask
from flask import render_template, session
import json
import urllib
from google.appengine.api import urlfetch 
#Code from https://developers.google.com/identity/protocols/OAuth2WebServer
app = flask.Flask(__name__)
CLIENT_ID = '[].apps.googleusercontent.com'
CLIENT_SECRET = '[]'
SCOPE = 'email'
REDIRECT_URI = 'https://[].appspot.com/oauthcallback'
#check for last oauth step, if not go to intermediate steps
@app.route('/')
def index():
    if 'credentials' not in flask.session:
        return flask.render_template('index.html')
    credentials = json.loads(flask.session['credentials'])
    if credentials['expires_in'] <= 0:
        return flask.redirect(flask.url_for('oauthcallback'))
    else:
        headers = {'Authorization': 'Bearer {}'.format(credentials['access_token'])}
        req_uri = 'https://www.googleapis.com/oauth2/v2/userinfo'
        r = requests.get(req_uri, headers=headers)
        return r.text
#ask user to sign in, send code to googleapi to get token
@app.route('/oauthcallback')
def oauthcallback():
    if 'code' not in flask.request.args:
        auth_uri = ('https://accounts.google.com/o/oauth2/v2/auth?response_type=code&client_id={}&redirect_uri={}&scope={}').format(CLIENT_ID, REDIRECT_URI, SCOPE)
        return flask.redirect(auth_uri)
    else:
        auth_code = flask.request.args.get('code')
        data = {'code': auth_code, 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'redirect_uri': REDIRECT_URI, 'grant_type': 'authorization_code'}
        r = urlfetch.fetch("https://www.googleapis.com/oauth2/v4/token", payload=data, method="POST")
        #return r.content #prints json
        flask.session['credentials'] = r.content #breaks here
        return flask.redirect(flask.url_for('index'))

if __name__ == '__main__':
    import uuid
    app.secret_key = str(uuid.uuid4())
    app.debug = True
    app.run()

是否启用了重定向 URI?

https://developers.google.com/api-client-library/python/auth/web-app

Create authorization credentials
Any application that uses OAuth 2.0 to access Google APIs must have authorization credentials that identify the application to Google's OAuth 2.0 server. The following steps explain how to create credentials for your project. Your applications can then use the credentials to access APIs that you have enabled for that project.
Open the Credentials page in the API Console.
Click Create credentials > OAuth client ID.
Complete the form. Set the application type to Web application. Applications that use languages and frameworks like PHP, Java, Python, Ruby, and .NET must specify authorized redirect URIs. The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses.
For testing, you can specify URIs that refer to the local machine, such as http://localhost:8080. With that in mind, please note that all of the examples in this document use http://localhost:8080 as the redirect URI. 

最新更新