授权和身份验证
我们强烈建议您在与 SAS Portal API 集成时使用服务帐号进行身份验证。如有必要,请创建服务帐号。在创建服务帐号密钥时,请务必选择 JSON 作为密钥类型。完成后,您的服务帐号密钥将下载到浏览器的默认位置。此外,请务必为服务帐号授予“Project Owner”角色。
接下来,您需要以 Bearer 令牌的形式提供服务帐号身份验证。如果您直接调用 SAS Portal API(例如使用 cURL
发出 HTTP 请求),您将在 Authorization
标头中以不记名令牌的形式传递身份验证请求。如需使用服务帐号获取不记名令牌,请按以下步骤操作:
- 安装
gcloud
命令行工具。
-
向您的服务帐号进行身份验证。在以下命令中,将 ${KEY_FILE} 替换为指向服务帐号密钥文件的路径:
gcloud auth activate-service-account --key-file ${KEY_FILE}
-
使用您的服务帐号获取授权令牌:
gcloud auth print-access-token
该命令会返回一个访问令牌值。
-
使用 API 时,请在 Authorization
标头中以不记名令牌的形式传递令牌值。请参阅以下示例:
curl -X GET -H "X-Goog-User-Project: ${CLIENT_PROJECT}" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${TOKEN}" \
"https://sasportal.googleapis.com/v1alpha1/customers"
将 ${CLIENT_PROJECT} 设置为发出请求的 Google Cloud 项目的 ID,然后将 ${TOKEN} 设置为授权令牌。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2022-09-26。
[null,null,["最后更新时间 (UTC):2022-09-26。"],[[["Service accounts are the recommended authentication method for integrating with the SAS Portal API, requiring the creation of a service account with a JSON key and granting it the \"Project Owner\" role."],["Authentication to the SAS Portal API is done using a Bearer token obtained through the `gcloud` command-line tool by activating the service account and printing an access token."],["When calling the API, include the Bearer token in the `Authorization` header of your request along with the `X-Goog-User-Project` header specifying your Google Cloud Project ID."]]],["The key actions are creating a service account with a JSON key, granting it the \"Project Owner\" role, and obtaining a Bearer token for API authentication. This is done by installing the `gcloud` tool, authenticating the service account using `gcloud auth activate-service-account --key-file`, and generating an access token via `gcloud auth print-access-token`. This token should then be included in the `Authorization` header when making API calls as a Bearer token.\n"]]