且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

POSTMAN:无法完成 OAuth2.0 登录

更新时间:2023-02-16 16:04:24

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

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

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

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

填写表格.

  • 回调网址: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}
  • 客户秘密:${你的线索秘密}
  • 范围:${您的范围},例如https://www.googleapis.com/auth/gmail.readonly 用于 Gmail 只读访问权限.确保您的项目中允许使用相应的范围和 API(例如,在同意屏幕配置页面上).

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

去使用你的令牌!

I want to request a token in order I can have my Postman logged in my Gmail acocount and then I can call Google Cloud APIs.

I am trying by typing my Gmail email address and its password as you can see from this picture

The final goal is get APIs like this

https://monitoring.googleapis.com/v1/projects/firetestjimis/dashboards

I can do it by login with my Gmail account from gcloud ("gcloud auth application-default login"), then print the token with gcloud auth application-default print-access-token, copy the printed token and paste it in Access Token (Postman). Well it works, but it is kind of obligating me to start gcloud and has it installed. I am pretty sure I can reach same idea by sign with same user I sign in gcloud. I just don't know how to do it with Postman. All I have read last hours drove me to the image I pasted above but it is failling with error from this question.

In case it is relevant, here is the gcloud console where I get the token

C:Program Files (x86)GoogleCloud SDK>gcloud auth application-default print-access-token
C:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdkin..lib	hird_partygoogleauth\_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)GoogleCloud SDK>

*** Edited a bit after firstly posted

Other tentative is resulting in

Erro 400: invalid_request
Invalid parameter value for redirect_uri: Missing scheme: /

When I tried

The Client Id I copied from

*** edited after Madhead's suggestion

I followed carefully all steps proposed and indeed I can get a Google Token straight from Postman by clicking on Get New Access Token. Nevetheless it gets

{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED"
  }
}

For these two tentatives from Postman (pasted curl script since it is easier to evaluated from others than a Postman printscreen)

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"}]
    }
}'

Here is the OAuth Client ID created in credentials screen

{"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"]}}

as downlowaded from

And here is the

*** edited

*** edited

  • edited

*** edited after succesfully added two scopes

It says "Because you've added a sensitive scope, your consent screen requires verification by Google before it's published"

And when I clicked in "Submit for verification" I see it complaining of Application Homepage and Application Privacy links

It's actually possible to use Postman to access OAuth 2.0 secured Google APIs.

First, you need to create an OAuth client in your Google Cloud Console project. It should be a "Web application" client, and the redirect URI should be https://oauth.pstmn.io/v1/callback:

Take a note of the Client ID and Client Secret:

Now, go to the Postman. Create new request. On the "Authorization" tab, select OAuth 2.0 and then click "Get New Access Token":

Fill in the form.

  • Callback URL: https://oauth.pstmn.io/v1/callback (will be set up automatically by Authorize using browser).
  • Authorize using browser: true
  • Auth URL: https://accounts.google.com/o/oauth2/auth
  • Access Token URL: https://accounts.google.com/o/oauth2/token
  • Client ID: ${your client id}
  • Client Secret: ${your cluent secret}
  • Scope: ${your scopes}, e.g. https://www.googleapis.com/auth/gmail.readonly for Gmail read-only access. Make sure that corresponding scopes and API's are allowed in your project (e.g. on consent scree configuration page).

Click "Request Token". The browser will open Google's OAuth consent screen of pstmn.io app. Log in if necessary and grant the access. At the end of the process, a pop-up will be opened (make sure it is not blocked by your browser), redirecting you back to the Postman app.

Go and use your token!