2017-03-04 70 views
0

开始使用Google登录保护我的应用程序(适用于我的域用户)。Google登录到应用程序(仅限域用户) - 正确设置

我使用这个作为一个整体参考: https://cloud.google.com/appengine/docs/standard/python/users/

我添加的代码到我的MainPage处理:

class MainPage(webapp2.RequestHandler): 
    def get(self): 
    user = users.get_current_user() 
     if user: 
      logout_url = users.create_logout_url('/') 
     else: 
      login_url = users.create_login_url('/') 
    template = JINJA_ENVIRONMENT.get_template('/templates/base.html') 
    self.response.out.write(template.render()) 

我更新的这个处理程序的app.yaml:

- url:/
    script: main.app 
    login: required 

我已经在Appengine本地进行了测试,登录页面的开发版本按预期出现。

第一个问题 - 我是否需要将代码添加到所有页面处理程序中,就像我为MainPage所做的那样?同样,我需要添加登录:app.yaml中的每个主要处理程序都需要登录吗?

在GCP云控制台中,我已经去了Project> AppEngine> Settings> Edit,如文档所示。编辑后,我现在有:

Google login cookie expiry Default (1 day) 
Referrers Google Apps domain: *********.com.au 
Email API authorised senders None 

第二个问题这是保证只有用户我的谷歌Apps域可以登录到这个应用程序的需要的呢?

我似乎目前在本地测试仍然可以登录,即使现在这已被限制为来自域的用户......这个限制只适用于应用程序部署到云端实时吗?

回答

0

处理OAuth 2.0(Google身份验证)的最简单方法是使用Google API客户端库提供的App Engine Python decorators。这些装饰器处理所有OAuth 2.0步骤,而无需使用任何Flow,Credentials或Storage对象。

  • OAuth2Decorator:使用OAuth2Decorator类去构造一个 装饰与你的客户ID和密码。

  • OAuth2DecoratorFromClientSecrets:使用
    OAuth2DecoratorFromClientSecrets类去构造使用()中描述flow_from_clientsecrets一个 client_secrets.json档案中的OAuth 2.0页面的 部装饰器。

请参考文件了解更多详情。

from apiclient.discovery import build 
from google.appengine.ext import webapp 
from oauth2client.contrib.appengine import OAuth2Decorator 

decorator = OAuth2Decorator(
    client_id='your_client_id', 
    client_secret='your_client_secret', 
    scope='https://www.googleapis.com/auth/calendar') 

service = build('calendar', 'v3') 

class MainHandler(webapp.RequestHandler): 

    @decorator.oauth_required 
    def get(self): 
    # Get the authorized Http object created by the decorator. 
    http = decorator.http() 
    # Call the service using the authorized Http object. 
    request = service.events().list(calendarId='primary') 
    response = request.execute(http=http) 
    ...