在 AppEngine 中验证 PubSub 推送消息
Authenticating PubSub Push messages in AppEngine
有没有办法确定应用引擎收到的消息来自 Google PubSub 服务?当前,PubSub 服务在 appengine app.yaml 中配置为 "login: admin" 的 URL 上获得 302。所以它一直在重试。
我原以为它会像 Appengine 中的任务一样运行,并自动向 "login:admin" 个 URL 进行身份验证。
FAQ 建议在设置您的 PubSub 推送订阅时,将共享秘密令牌作为您在处理程序中检查的请求参数。
If you additionally would like to verify that the messages originated from Google Cloud Pub/Sub, you could configure your endpoint to only accept messages that are accompanied by a secret token argument, for example,
https://myapp.mydomain.com/myhandler?token=application-secret.
由于 PubSub 不使用 appengine 身份验证,而我们使用令牌进行身份验证,因此您不应在该处理程序的 app.yaml 条目中指定 login
密钥。这是一个例子:
main.py
class Handler(webapp2.RequestHandler):
def post(self):
token = self.request.params['token']
if token != 'foo':
self.abort(401, 'Not authorized')
# do stuff
app = webapp2.WSGIApplication([
('/', Handler),
], debug=True)
app.yaml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app
有没有办法确定应用引擎收到的消息来自 Google PubSub 服务?当前,PubSub 服务在 appengine app.yaml 中配置为 "login: admin" 的 URL 上获得 302。所以它一直在重试。
我原以为它会像 Appengine 中的任务一样运行,并自动向 "login:admin" 个 URL 进行身份验证。
FAQ 建议在设置您的 PubSub 推送订阅时,将共享秘密令牌作为您在处理程序中检查的请求参数。
If you additionally would like to verify that the messages originated from Google Cloud Pub/Sub, you could configure your endpoint to only accept messages that are accompanied by a secret token argument, for example,
https://myapp.mydomain.com/myhandler?token=application-secret.
由于 PubSub 不使用 appengine 身份验证,而我们使用令牌进行身份验证,因此您不应在该处理程序的 app.yaml 条目中指定 login
密钥。这是一个例子:
main.py
class Handler(webapp2.RequestHandler):
def post(self):
token = self.request.params['token']
if token != 'foo':
self.abort(401, 'Not authorized')
# do stuff
app = webapp2.WSGIApplication([
('/', Handler),
], debug=True)
app.yaml
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /.*
script: main.app