简体   繁体   English

Postman - 如何在 Postman 中调用使用“使用 Google 登录”的 API 端点

[英]Postman - how to call API endpoint that uses "Sign in with Google" in Postman

I have an express app that uses express-openid-connect to authenticate requests via Google OIDC.我有一个快速应用程序,它使用express-openid-connect通过 Google OIDC 对请求进行身份验证。 It works fine and behaves as expected.它工作正常并按预期运行。 If I do not have a current session, then accessing any route in a browser will redirect to a Google login page.如果我没有当前的 session,那么在浏览器中访问任何路由都会重定向到 Google 登录页面。 Subsequent requests on any route will use the session cookie and will not redirect.任何路由上的后续请求都将使用 session cookie,并且不会重定向。 All's well.一切都好。

I want to call one of the routes defined in the app via Postman.我想通过 Postman 调用应用程序中定义的路线之一。 To do that I would expect to need to login to create a session and get a cookie.为此,我预计需要登录以创建 session 并获取 cookie。 If I grab the cookie from a browser session and manually edit the cookie in Postman to include the cookie contents then it works and I can call the endpoint from Postman.如果我从浏览器 session 获取 cookie 并手动编辑 Postman 中的 cookie 以包含 cookie 内容,那么它可以工作,我可以从 Postman 调用端点。 I'd like to avoid having to do this since it's so clunky and manual.我想避免这样做,因为它是如此笨重和手动。

I cannot find anything that says how to call an API endpoint that requires Google auth, in Postman.我在 Postman 中找不到任何说明如何调用需要 Google 身份验证的 API 端点的信息。 Everything I can find about Postman and Google auth, OIDC, etc. is about getting access tokens that the app can use to subsequently access other APIs.我能找到的关于 Postman 和 Google auth、OIDC 等的所有信息都是关于获取应用程序可用于随后访问其他 API 的访问令牌。 This is not what I'm doing.这不是我正在做的。 I just need to use Postman to call an endpoint that requires Google auth.我只需要使用 Postman 来调用需要谷歌身份验证的端点。

Is this possible in Postman?这在 Postman 中是否可行?

It is not easy steps but I try it step by step.这不是简单的步骤,但我一步一步地尝试。

Postman can do Authorization Code Grant without programming Postman 无需编程即可进行Authorization Code Grant

Two parts Setting两部分设置

1 Google Project Setting 1 Google Project Setting

  • Redirect URI, Client ID and Client Secret重定向 URI、客户端 ID 和客户端密码

2 Postman Get Token Settings 2 Postman Get Token Settings

  • Grant Type, Callback URL, Auth URL, Token URL, Client ID, Client Secret, Scope Grant Type, Callback URL, Auth URL, Token URL, Client ID, Client Secret, Scope

After Setting # 1设置 #1 后在此处输入图像描述

After Setting # 2设置 #2 后在此处输入图像描述

#1's 2,3 should be match #2's 2,3 #1 的 2,3 应该匹配 #2 的 2,3

if click Get New Access Token , then get the google login screen如果单击Get New Access Token ,则获取 google 登录屏幕

在此处输入图像描述

Next will show OAuth consent screen接下来将显示OAuth consent screen

在此处输入图像描述

if click the Allow button, get the access如果单击“ Allow ”按钮,则获得访问权限在此处输入图像描述

在此处输入图像描述

This is example of GET use information call with token.这是带有令牌的 GET 使用信息调用的示例。

https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token

在此处输入图像描述

Detail Steps for Setting #1设置 #1 的详细步骤

1 Go Google Develop Console 1 Go 谷歌开发控制台

https://console.cloud.google.com/products

2 Search new project 2 搜索新项目

在此处输入图像描述

3 Create New project 3 创建新项目

在此处输入图像描述

4 Create Credential 4 创建凭证

在此处输入图像描述

select OAuth Client ID select OAuth 客户 ID

在此处输入图像描述

Click Config Consent Screen单击Config Consent Screen

在此处输入图像描述

Select External User Type, Click Create button Select 外部用户类型,点击Create按钮

在此处输入图像描述

5 Edit App Registration 5 编辑应用注册

app-name, user support email, developer e-mail then click save and continue app-name,用户支持 email,开发者邮箱然后点击save and continue

在此处输入图像描述

Add Scope - important You can add your want to access item by adding scopes添加 Scope - 重要您可以通过添加范围来添加您想要访问的项目

I added simple scope to get user info我添加了简单的 scope 来获取用户信息

https://www.googleapis.com/auth/userinfo.email

在此处输入图像描述

在此处输入图像描述 Then click save and continue然后点击save and continue

Add Test User添加测试用户在此处输入图像描述 get the summary screen获取摘要屏幕

6 Create Client ID Use Authorized redirect URI this for Postman 6 为 Postman 创建客户端 ID 使用授权重定向 URI

https://www.getpostman.com/oauth2/callback

在此处输入图像描述

Then google will create credential (Client ID and Secret)然后谷歌将创建凭据(客户端 ID 和秘密)

在此处输入图像描述

Detail Steps for Setting #2设置 #2 的详细步骤

In Postman, Select Authorization Tab, select type is OAuth 2.0在 Postman、Select Authorization选项卡中,select 类型为OAuth 2.0

2 (Client ID),3 (Client Secret) use Setting #1's credential 2(客户端 ID),3(客户端密码)使用设置 #1 的凭据

4 Grant Type is Authorization Code 4 授权类型是Authorization Code

5 Callback URL 5 回调URL

https://www.getpostman.com/oauth2/callback

6 Auth URL 6 认证 URL

https://accounts.google.com/o/oauth2/v2/auth

7 Access Token URL 7 访问令牌 URL

https://oauth2.googleapis.com/token

8 Scope 8 Scope

https://www.googleapis.com/auth/userinfo.email

Then Click New Access Token Login screen will comes as upper images然后单击New Access Token Login 屏幕将如上图所示

That is it, I hope to follow all steps, to figure out an Authorization Code flow by Postman.就是这样,我希望按照所有步骤,通过 Postman 找出Authorization Code流程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM