使用服务账号进行身份验证
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
前提条件
本页面假定您已经完成以下操作:
配置身份验证
要使用服务账号进行身份验证,请执行以下操作:
将 App Engine Endpoints API 导入您的 API 类中:
import endpoints
将服务账号的 issuer 对象添加到 API 修饰器。
例如:
@endpoints.api(
name='echo',
version='v1',
issuers={'serviceAccount': endpoints.Issuer(
'YOUR_SERVICE_ACCOUNT_EMAIL',
'https://www.googleapis.com/robot/v1/metadata/x509/YOUR_SERVICE_ACCOUNT_EMAIL')},
audiences={'serviceAccount': ['YOUR_AUDIENCE']})
- 将
echo
替换为您的 API 名称。
- 将
v1
替换为您的 API 版本。
- 将
YOUR_SERVICE_ACCOUNT_EMAIL
替换为您的服务账号电子邮件地址。
- 将
YOUR_AUDIENCE
替换为调用服务发送的 aud
字段中的值。
在需要检查身份验证是否正确的每种 API 方法中,检查是否存在有效的 User
,如果不存在,则引发错误 401
,具体如以下方法定义示例中所示:
user = endpoints.get_current_user()
# If there's no user defined, the request was unauthenticated, so we
# raise 401 Unauthorized.
部署 API。每当添加新客户端时,您都需要重新部署 API。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-12-10。
[{
"type": "thumb-down",
"id": "hardToUnderstand",
"label":"Hard to understand"
},{
"type": "thumb-down",
"id": "incorrectInformationOrSampleCode",
"label":"Incorrect information or sample code"
},{
"type": "thumb-down",
"id": "missingTheInformationSamplesINeed",
"label":"Missing the information/samples I need"
},{
"type": "thumb-down",
"id": "translationIssue",
"label":"翻译问题"
},{
"type": "thumb-down",
"id": "otherDown",
"label":"其他"
}]
[{
"type": "thumb-up",
"id": "easyToUnderstand",
"label":"易于理解"
},{
"type": "thumb-up",
"id": "solvedMyProblem",
"label":"解决了我的问题"
},{
"type": "thumb-up",
"id": "otherUp",
"label":"其他"
}]