google-cloud-platform - postman :无法完成 OAuth2.0 登录

标签 google-cloud-platform google-api postman gcloud google-oauth

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

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

Postman Request Token

最终目标是获得这样的 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: /

当我尝试

Postman

我复制的客户端 ID

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

*** 根据 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"]}}

来自

Google Cloud Plataform Credentials

这是 OAuth consent screen

*** 已编辑

Add Scorpe screen

scope invalid

*** 已编辑

trying add https://monitoring.googleapis.com/ as scope
  • 已编辑

  • Nothing returned from Cloud Monitoring API

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

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

    waiting for Google aproval

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

    complaining about two urls

    最佳答案

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

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

    Create OAuth client ID

    记下客户端 ID 和客户端 key :

    Client ID & Client Secret

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

    New request

    填写表格。

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

  • New access token
    Consent screen & Scopes
    Monitoring scopes

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

    去使用你的代币吧!

    Token usage

    关于google-cloud-platform - postman :无法完成 OAuth2.0 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61622038/

    相关文章:

    ruby - 不要使用 google api ruby​​ 客户端请求离线访问

    firebase - 在本地托管的 Firebase 应用程序中使用本地模拟的 https.onCall Firebase 函数

    java - 在java中设置谷歌云存储桶访问日志

    ruby-on-rails - 谷歌管理目录插入用户

    java - 使用 Google API Java 客户端上传到 YouTube 时出现问题

    java - 使用 POSTMAN 向 Java REST API 发送 POST 请求并在 xml 响应中获取空值

    ios - 如何使用 Alamofire 像 Postman 一样发送表单数据

    php - POST 适用于 Postman,但不适用于 CURL

    kubernetes - 如何在 Terraform 中创建 GCP 工作负载身份 IAM 绑定(bind)?

    node.js - node.js 上的 gcloud pubsub - 如何设置 IAM 策略