我在 Google App Engine 中有一个应用程序,包含 2 个模块(A
and B
). A
处理用户请求并且无需身份验证即可使用。B
是一个微服务,在以下情况下执行某些任务A
需要它。所以我们有A
向B
using urlfetch
:
from google.appengine.api import urlfetch
from google.appengine.api import app_identity
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(
rpc,
"https://b-dot-my-project.appspot.com/some/url",
method='GET',
follow_redirects=False,
headers = {
'X-Appengine-Inbound-Appid': 'my-project',
},
)
response = rpc.get_result()
B
's app.yaml
看起来像:
runtime: python27
api_version: 1
threadsafe: yes
service: b
handlers:
- url: /.*
script: my_module.app
login: admin
auth_fail_action: unauthorized
In the docs https://cloud.google.com/appengine/docs/python/issue-requests#issuing_a_request_to_another_app_engine_app,他们建议:
当向另一个 App Engine 应用程序发出请求时,您的 App Engine 应用程序
必须通过添加标头来声明其身份
X-Appengine-Inbound-Appid 到请求。如果您指示 URL
获取服务不遵循重定向,App Engine 将添加此标头
自动请求。
无论我做什么,我都会不断得到401
当提出这个要求时。两个都A
and B
部署在同一个项目中。尝试设置follow_redirects=False
并添加标题X-Appengine-Inbound-Appid
手动(虽然我没想到它会因为描述的原因起作用here https://cloud.google.com/appengine/docs/python/appidentity/?csw=1#Asserting_Identity_to_Other_Systems#asserting_identity_to_other_app_engine_apps),仍然不确定是否设置了标头,因为日志B
不包含请求标头,并且失败情况发生在我的处理程序模块执行之前。
如果可以的话我宁愿依靠A
验证到B
而不是仅仅放弃该选项login: admin
并且仅依赖标头,因为能够调用会更好B
来自项目管理员帐户(例如用于调试目的)。