我想请求一个 token ,以便我可以让我的 Postman 登录我的 Gmail 帐户,然后我可以调用 Google Cloud API。
我正在尝试输入我的 Gmail 电子邮件地址及其密码,正如您从这张图片中看到的
最终目标是获得这样的 API
https://monitoring.googleapis.com/v1/projects/firetestjimis/dashboards
我可以通过从 gcloud 登录我的 Gmail 帐户(“gcloud auth 应用程序默认登录”)来完成,然后使用 gcloud auth 应用程序默认打印访问 token 打印 token ,复制打印的 token 并将其粘贴到访问 token 中( postman )。好吧,它工作正常,但我有义务启动 gcloud 并安装它。我很确定我可以通过与我在 gcloud 中登录的同一用户签名来达成相同的想法。我只是不知道如何用 Postman 做到这一点。上几个小时我读到的所有内容都让我看到了上面粘贴的图像,但由于这个问题的错误而失败。
如果相关,这是我获取 token 的 gcloud 控制台
C:\Program Files (x86)\Google\Cloud SDK>gcloud auth application-default print-access-token
C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin\..\lib\third_party\google\auth\_default.py:69: UserWarning: Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/
warnings.warn(_CLOUD_SDK_CREDENTIALS_WARNING)
ya29.a0Ae4lvC3-3WxUmx... removed token ... hs251yTql3eEE
C:\Program Files (x86)\Google\Cloud SDK>
*** 首次发布后稍作修改
其他暂定导致
Erro 400: invalid_request
Invalid parameter value for redirect_uri: Missing scheme: /
当我尝试
我复制的客户端 ID
*** 根据 Madhead 的建议编辑
我仔细遵循了所有建议的步骤,实际上我可以通过单击“获取新访问 token ”直接从 Postman 获取 Google token 。尽管如此它得到
{
"error": {
"code": 403,
"message": "Request had insufficient authentication scopes.",
"status": "PERMISSION_DENIED"
}
}
对于 Postman 的这两个暂定(粘贴 curl 脚本,因为它比 Postman 打印屏幕更容易从其他人那里评估)
curl --location --request GET 'https://monitoring.googleapis.com/v1/projects/firetestjimis/dashboards' --header 'Authorization: Bearer ya29. *** token *** kO3Fg'
curl --location --request POST 'https://firestore.googleapis.com/v1/projects/firetestjimis/databases/(default)/documents:runQuery' --header 'Authorization: Bearer ya29. *** token *** kO3Fg' --header 'Content-Type: application/json' --data-raw '{
"structuredQuery": {
"where" : {
"fieldFilter" : {
"field": {"fieldPath": "id"},
"op":"EQUAL",
"value": {"stringValue": "1"}
}
},
"from": [{"collectionId": "transfer"}]
}
}'
这是在 credentials screen 中创建的 OAuth 客户端 ID
{"web":{"client_id":"7953 *** 5k6e9ivtdg.apps.googleusercontent.com","project_id":"firetestjimis","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"IJGCe *** Du6bU","redirect_uris":["https://oauth.pstmn.io/v1/callback"]}}
来自
这是
*** 已编辑
*** 已编辑
*** 成功添加两个范围后编辑
它说“因为你添加了一个敏感范围,你的同意屏幕需要谷歌验证才能发布”
当我点击“提交验证”时,我看到它提示应用程序主页和应用程序隐私链接
最佳答案
实际上可以使用 Postman 访问受 OAuth 2.0 保护的 Google API。
首先,您需要在您的 Google Cloud Console 项目中创建一个 OAuth 客户端。它应该是一个“Web 应用程序”客户端,并且重定向 URI 应该是 https://oauth.pstmn.io/v1/callback
:
记下客户端 ID 和客户端 key :
现在,去 postman 那里。创建新请求。在“授权”选项卡上,选择 OAuth 2.0,然后单击“获取新访问 token ”:
填写表格。
https://oauth.pstmn.io/v1/callback
(将由 Authorize using browser
自动设置)。 https://accounts.google.com/o/oauth2/auth
https://accounts.google.com/o/oauth2/token
https://www.googleapis.com/auth/gmail.readonly
用于 Gmail 只读访问。确保您的项目中允许相应的范围和 API(例如,在同意屏幕配置页面上)。 单击“请求 token ”。浏览器将打开 Google 的 OAuth 同意屏幕
pstmn.io
应用程序。如有必要,请登录并授予访问权限。在该过程结束时,将打开一个弹出窗口(确保它没有被您的浏览器阻止),将您重定向回 Postman 应用程序。去使用你的代币吧!
关于google-cloud-platform - postman :无法完成 OAuth2.0 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61622038/