App Engine Python身份验证教程破了吗?

时间:2018-03-24 03:06:43

标签: python google-app-engine flask oauth-2.0

我正在尝试使用App Engine Authenticating Users with Python教程。我使用谷歌自己的教程文件非常仔细地遵循了指示。登录后,显然没有创建会话变量;根据base.html中的以下代码,我应该看到我的名字(因为我没有存储个人资料图片):

{% if session.profile %}
  <a href="/logout">
    {% if session.profile.image %}
      <img class="img-circle" src="{{session.profile.image.url}}" width="24">
    {% endif %}
    {{session.profile.displayName}}
  </a>
{% else %}
  <a href="/oauth2authorize">Login</a>
{% endif %}

当我点击“我的图书”链接(href =“/ books / mine”)时,出现错误:KeyError'profile'。 / mine的处理程序是:

    @crud.route("/mine")
    @oauth2.required
    def list_mine():
        token = request.args.get('page_token', None)
        if token:
            token = token.encode('utf-8')

        books, next_page_token = get_model().list_by_user(
            user_id=session['profile']['id'],
            cursor=token)

        return render_template(
            "list.html",
            books=books,
            next_page_token=next_page_token)

请求凭据并将其传递到__init__.py文件中的Flask会话变量:

def _request_user_info(credentials):
    """
    Makes an HTTP request to the Google+ API to retrieve the user's basic
    profile information, including full name and photo, and stores it in the
    Flask session.
    """
    http = httplib2.Http()
    credentials.authorize(http)
    resp, content = http.request(
        'https://www.googleapis.com/plus/v1/people/me')

    if resp.status != 200:
        current_app.logger.error(
            "Error while obtaining user profile: \n%s: %s", resp, content)
        return None
    session['profile'] = json.loads(content.decode('utf-8'))

回溯指向crud.py的第66行,在上面的代码中为user_id=session['profile']['id'],

以下是最后一系列追溯条目:

  File "/User/Joe/Documents/GitHub/appenginedeepdive-master/4-auth/env/lib/python3.6/site-packages/oauth2client/contrib/flask_util.py", line 524, in required_wrapper
    return wrapped_function(*args, **kwargs)
  File "/User/Joe/Documents/GitHub/appenginedeepdive-master/4-auth/bookshelf/crud.py", line 66, in list_mine
    user_id=session['profile']['id'],
  File "/User/Joe/Documents/GitHub/appenginedeepdive-master/4-auth/env/lib/python3.6/site-packages/werkzeug/local.py", line 377, in <lambda>
    __getitem__ = lambda x, i: x._get_current_object()[i]
KeyError: 'profile'

我尝试在执行应用程序之前运行'gcloud auth login',即使说明没有要求你这样做,但我想不出其他任何事情要尝试。我已经尝试了本地服务器(python main.py)和部署到App Engine,但得到了同样的错误。

检查Chrome的开发者工具,没有创建任何会话变量,但我无法弄清楚为什么不。也许有一些与我没有得到的Flask框架有关。

任何人都有任何想法发生了什么?

谢谢 - 乔

0 个答案:

没有答案