邮递员:无法完成 OAuth2.0 登录

Jim*_*m C 3 google-api google-oauth google-cloud-platform postman gcloud

我想请求一个令牌,以便我可以让我的 Postman 登录我的 Gmail 帐户,然后我可以调用 Google Cloud API。

我正在尝试输入我的 Gmail 电子邮件地址及其密码,正如您从这张图片中看到的

邮递员请求令牌

最终目标是获得这样的 API

https://monitoring.googleapis.com/v1/projects/firetestjimis/dashboards
Run Code Online (Sandbox Code Playgroud)

我可以通过从 gcloud 登录我的 Gmail 帐户(“gcloud auth 应用程序默认登录”)来完成,然后使用 gcloud auth 应用程序默认打印访问令牌打印令牌,复制打印的令牌并将其粘贴到访问令牌中(邮差)。好吧,它工作正常,但我有义务启动 gcloud 并安装它。我很确定我可以通过与我在 gcloud 中登录的同一用户签名来达成相同的想法。我只是不知道如何用 Postman 做到这一点。上几个小时我读到的所有内容都让我看到了上面粘贴的图像,但由于这个问题的错误而失败。

如果相关,这是我获取令牌的 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>
Run Code Online (Sandbox Code Playgroud)

*** 首次发布后稍作修改

其他暂定导致

Erro 400: invalid_request
Invalid parameter value for redirect_uri: Missing scheme: /
Run Code Online (Sandbox Code Playgroud)

当我尝试

邮差

我复制的客户端 ID

https://console.developers.google.com/apis/credentials

*** 根据 Madhead 的建议编辑

我仔细遵循了所有建议的步骤,实际上我可以通过单击“获取新访问令牌”直接从 Postman 获取 Google 令牌。尽管如此它得到

{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED"
  }
}
Run Code Online (Sandbox Code Playgroud)

对于 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"}]
    }
}'
Run Code Online (Sandbox Code Playgroud)

这是在凭据屏幕中创建的 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"]}}
Run Code Online (Sandbox Code Playgroud)

来自

谷歌云平台凭据

这里是 OAuth 同意屏幕

*** 已编辑

添加蝎子屏幕

范围无效

*** 已编辑

尝试添加 https://monitoring.googleapis.com/ 作为范围

  • 已编辑

Cloud Monitoring API 没有返回任何内容

*** 成功添加两个范围后编辑

它说“因为你添加了一个敏感范围,你的同意屏幕需要谷歌验证才能发布”

等待谷歌批准

当我点击“提交验证”时,我看到它抱怨应用程序主页和应用程序隐私链接

抱怨两个网址

mad*_*ead 6

实际上可以使用 Postman 访问受 OAuth 2.0 保护的 Google API。

首先,您需要在您的 Google Cloud Console 项目中创建一个 OAuth 客户端。它应该是一个“Web 应用程序”客户端,并且重定向 URI 应该是https://oauth.pstmn.io/v1/callback

创建 OAuth 客户端 ID

记下客户端 ID 和客户端密钥:

客户端 ID 和客户端密码

现在,去邮递员那里。创建新请求。在“授权”选项卡上,选择 OAuth 2.0,然后单击“获取新访问令牌”:

新请求

填写表格。

  • 回调 URL:(https://oauth.pstmn.io/v1/callback将由 自动设置Authorize using browser)。
  • 使用浏览器授权:true
  • 验证网址: https://accounts.google.com/o/oauth2/auth
  • 访问令牌网址: https://accounts.google.com/o/oauth2/token
  • 客户 ID:${您的客户 ID}
  • 客户端秘密:${你的线索秘密}
  • 范围:${your scopes},例如https://www.googleapis.com/auth/gmail.readonly用于 Gmail 只读访问。确保在您的项目中允许相应的范围和 API(例如在同意屏幕配置页面上)。

新访问令牌 同意屏幕和范围 监控范围

单击“请求令牌”。浏览器将打开pstmn.io应用程序的Google 的 OAuth 同意屏幕。如有必要,请登录并授予访问权限。在该过程结束时,将打开一个弹出窗口(确保它没有被您的浏览器阻止),将您重定向回 Postman 应用程序。

去使用你的代币吧!

代币使用