如何使用Postman和HTTPS使用Google服务帐户凭据进行身份验证?

o2g1uqev  于 5个月前  发布在  Postman
关注(0)|答案(3)|浏览(72)

我读过这些文件:

  • https://cloud.google.com/docs/authentication/getting-started?hl=ru#auth-cloud-implicit-java
  • https://cloud.google.com/storage/docs/reference/libraries?hl=ru
  • https://cloud.google.com/docs/authentication/production?hl=ru#obtaining_and_providing_service_account_credentials_manually
  • https://developers.google.com/identity/protocols/OAuth2ServiceAccount#callinganapi

我已经存储了服务帐户的google-secrets.json凭据。但是我不明白如何发送带有凭据的请求以获取auth token或API密钥。
只有出版的图书馆才能做到这一点。
如何在Postman中设置HTTPS身份验证?

8ehkhllq

8ehkhllq1#

Gordon Thompson在一篇关于Google Authentication with Postman的文章中很好地回答了这一问题,文章中介绍了三种不同的方法:

  • 使用oauth2l CLI
  • 使用OAuth 2和用户凭据配置Postman
  • 将Postman配置为使用预请求脚本。这将从服务帐户密钥自动生成Google Access和ID令牌,并将其保存在Postman中。基于Denis Loginov的工作,要点可在here中获得:
yiytaume

yiytaume2#

found.
在JWT.io上使用RS256进行签名,参数为
标题:

{
  "alg": "RS256",
  "typ": "JWT",
  "kid": "your_private_key_id"
}

字符串
有效载荷:

{
  "iss": "your_client_email_of_service_account",
  "sub": "your_client_email_of_service_account",
  "aud": "https://www.googleapis.com/oauth2/v4/token",
  "scope": "https://www.googleapis.com/auth/devstorage.read_only",
  "iat": current_unix_time,
  "exp": current_unix_time+3600
}


验证签名
your_private_key在最后一个字段中(没有\n)
得到编码的密钥
然后

POST /oauth2/v4/token HTTP/1.1

Host: www.googleapis.com

Content-Type: application/x-www-form-urlencoded

Cache-Control: no-cache

grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=ENCODED_KEY_FROM_JWT.IO


我得到了回应

{

    "access_token": "ya21.c.ElrSBodwuWANeh7Q0-zlXpOxqm9-uEszPElsY2tvoG5aPxRgOkasN5G2sMgj3iosPVbRYk1wXw_DcBnm2FtuNBlZpv_wCC0YS5pWMykR8Ouf5CZg-8OK842rvfk",

    "expires_in": 3600,

    "token_type": "Bearer"

}


另外,不要忘记给予您的服务帐户在存储中读取的权限

dfuffjeb

dfuffjeb3#

在Postman应用程序中
1.转到 Environments 模块并创建一个新的环境。我们将其命名为 gcp_sa_env
1.创建一个名为 serviceAccountKey 的新变量,并将 google-secrets.json 的内容复制粘贴到它的值中。JSON应该包含像client_emailprivate_keytoken_uri这样的项目。
1.转到 Collections 模块并创建新请求
1.在右上角选择创建的环境(gcp_sa_env
1.在 Pre-request Script 中,复制粘贴此JavaScript代码(gist),以便从服务帐户密钥自动生成Google OAuth令牌
1.根据需要在SCOPES变量中添加或删除作用域
1.在 Authorization 模块中,授权类型选择 Bearer Token,*Token * 写{{accessToken}}
1.配置请求的其余部分(方法、URL、主体等)并运行它

相关问题