反向工程gcloud工具--主要是使用--log-http --我能够获得生成的auth令牌,甚至选择了相同的范围(在https://www.googleapis.com/oauth2/v1/tokeninfo上进行双重检查):
http://[redacted]/callback?state=[redacted]&code=[redacted]&scope=email+openid+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcloud-platform+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Faccounts.reauth&authuser=0&prompt=consent
https://oauth2.googleapis.com:443/token?grant_type=authorization_code&code=[redacted]&redirect_uri=[redacted]/callback&client_id=[redacted]apps.googleusercontent.com&client_secret=[redacted]我尝试用以下方式点击端点的标题:
Content-Type: application/json
charset: utf-8
Authorization: Bearer [redacted]
X-Goog-User-Project: [redacted]我还尝试在查询字符串中添加access_token=[redacted]。但不管我做什么,我总是得到:
{
"error": {
"code": 401,
"message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"errors": [
{
"message": "Login Required.",
"domain": "global",
"reason": "required",
"location": "Authorization",
"locationType": "header"
}
],
"status": "UNAUTHENTICATED"
}
}像/projects/list这样的其他端点似乎可以使用此访问令牌(在标头中)。我做错了什么?
发布于 2021-09-27 20:52:03
原来我的解决方案是100%正确的。由于一些奇怪的原因,保存在我的struct的访问令牌字段中的字符串不同于从远程auth流中收集的字符串。
发布于 2021-08-27 14:36:58
正如我从错误消息中看到的,您得到了401 http状态代码。这意味着您使用的访问令牌不允许您访问(未授权)列出区域。,这里您需要知道,您无法比较、zone.list、和 projects.list。事实上,
compute.zones.list权限,就像Compute Viewer一样,而projects.list需要有很多角色的resourcemanager.projects.list,即使是从权限数量来看,Browser角色也是最小的角色(我认为)。然后,下面这句话引起了我对您如何获得访问令牌的关注:
逆向工程的gcloud工具
您需要知道获取访问令牌非常容易。
gcloud auth login,它通过基于web的授权流获得用户帐户的访问凭据。gcloud auth print-access-token,它为您的用户帐户打印访问令牌,因此他的权限。因此,如果您的用户帐户具有compute.zones.list和resourcemanager.projects.list权限的角色(例如,Compute Viewer角色两者兼具),您将能够成功地调用两个端点。
最后,下面是一个curl示例,清单区域,使用生成的访问令牌:
curl -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" https://compute.googleapis.com/compute/v1/projects/<PROJECT_ID>/zoneshttps://stackoverflow.com/questions/68951860
复制相似问题