Google 登录应用程序(仅限域用户)- 正确设置
Google login to app (domain users only) - correct setup
开始使用 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 中那样将代码添加到我的所有页面处理程序中?同样,我是否需要将 login:required 添加到 app.yaml 中的每个主要处理程序?
在 GCP Cloud Console 中,我按照文档的建议转到项目>AppEngine>设置>编辑。编辑后我现在有:
Google login cookie expiry Default (1 day)
Referrers Google Apps domain: *********.com.au
Email API authorised senders None
第二个问题这是确保只有来自我的 Google 应用程序域的用户才能登录此应用程序所需的全部吗?
我目前在本地测试时似乎仍然能够登录,即使现在这已被限制为域中的用户...此限制是否仅在应用程序实时部署到云后才适用?
处理 OAuth 2.0(Google 身份验证)的最简单方法是使用 Google API 客户端库提供的 App Engine Python decorators。这些装饰器处理所有 OAuth 2.0 步骤,您无需使用任何 Flow、Credentials 或 Storage 对象。
OAuth2Decorator:使用OAuth2Decoratorclass构造一个
带有您的客户端 ID 和密码的装饰器。
OAuth2DecoratorFromClientSecrets:使用
OAuth2DecoratorFromClientSecrets class 构造装饰器使用
在 flow_from_clientsecrets() 中描述的 client_secrets.json 文件
OAuth 2.0 页面的一部分。
请参阅 this 文档了解更多详情。
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)
...
开始使用 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 中那样将代码添加到我的所有页面处理程序中?同样,我是否需要将 login:required 添加到 app.yaml 中的每个主要处理程序?
在 GCP Cloud Console 中,我按照文档的建议转到项目>AppEngine>设置>编辑。编辑后我现在有:
Google login cookie expiry Default (1 day)
Referrers Google Apps domain: *********.com.au
Email API authorised senders None
第二个问题这是确保只有来自我的 Google 应用程序域的用户才能登录此应用程序所需的全部吗?
我目前在本地测试时似乎仍然能够登录,即使现在这已被限制为域中的用户...此限制是否仅在应用程序实时部署到云后才适用?
处理 OAuth 2.0(Google 身份验证)的最简单方法是使用 Google API 客户端库提供的 App Engine Python decorators。这些装饰器处理所有 OAuth 2.0 步骤,您无需使用任何 Flow、Credentials 或 Storage 对象。
OAuth2Decorator:使用OAuth2Decoratorclass构造一个 带有您的客户端 ID 和密码的装饰器。
OAuth2DecoratorFromClientSecrets:使用
OAuth2DecoratorFromClientSecrets class 构造装饰器使用 在 flow_from_clientsecrets() 中描述的 client_secrets.json 文件 OAuth 2.0 页面的一部分。
请参阅 this 文档了解更多详情。
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)
...