在GAE上实现Google与openId Connect的连接

时间:2015-03-31 21:30:32

标签: python google-app-engine openid-connect

我正在尝试编写一个基本的GAE应用来测试OpenId Connect。它失败了日志中的错误:people()。get()上的“权限不足”。似乎这个程序要求OpenId凭据而不是OAuth(在登录页面url中有一个重定向到https://accounts.google.com/o/openid2/auth。但这是隐含的。如何明确要求OAuth?这很奇怪,因为decorator.has_credentials( )使用这些openId凭据返回True ...

import logging
import webapp2
from apiclient.discovery import build
from oauth2client.appengine import OAuth2Decorator
from google.appengine.api import users

decorator = OAuth2Decorator(
  client_id='123456789999.....googleusercontent.com',
  client_secret='ABCDEF.........',
  scope=['https://www.googleapis.com/auth/plus.login'])


service = build('plus', 'v1')

class MainHandler(webapp2.RequestHandler):

  @decorator.oauth_aware
  def get(self):
    if decorator.has_credentials():
        response =service.people().get(userId="me").execute(http=decorator.http())
        # Write the profile data
        self.response.write(unicode(response))
    else:
        url = decorator.authorize_url()
        # Write a page explaining why authorization is needed,
        # and provide the user with a link to the url to proceed.
        # When the user authorizes, they get redirected back to this path,
        # and has_credentials() returns True.
        self.response.write('You must login : <a href="'+url+'">Go</a>')


app = webapp2.WSGIApplication([
                           ('/', MainHandler),
                           (decorator.callback_path, decorator.callback_handler())],
                          debug=True)

1 个答案:

答案 0 :(得分:0)

您必须在Google控制台http://code.google.com/apis/console中激活Google+ API。

API&amp; Auth / API / Google+ API

然后点击带有文字的按钮:&#34;启用API&#34;