Share How to OAuth 2.0 Authorization with Postman

## Cách thức ủy quyền của OAuth 2.0 với người đưa thư

OAuth 2.0 là một khung ủy quyền phổ biến cho phép người dùng cấp các ứng dụng của bên thứ ba truy cập vào tài nguyên được bảo vệ của họ mà không phải chia sẻ mật khẩu của họ.Điều này được thực hiện bằng cách phát hành mã thông báo truy cập, đây là thông tin xác thực ngắn gọn để cấp cho ứng dụng giới hạn quyền truy cập vào tài nguyên của người dùng.

Postman là một công cụ phát triển API phổ biến có thể được sử dụng để kiểm tra và tài liệu API.Nó cũng bao gồm luồng ủy quyền OAuth 2.0 tích hợp, giúp cho việc tích hợp OAuth 2.0 vào các ứng dụng API của bạn dễ dàng.

Trong hướng dẫn này, chúng tôi sẽ chỉ cho bạn cách sử dụng ủy quyền OAuth 2.0 với Postman.Chúng tôi sẽ tạo một API đơn giản cho phép người dùng tạo và liệt kê các tác vụ.Sau đó, chúng tôi sẽ sử dụng Postman để tạo mã thông báo truy cập và sử dụng nó để truy cập các tài nguyên được bảo vệ của API.

### Điều kiện tiên quyết

Để làm theo hướng dẫn này, bạn sẽ cần những điều sau đây:

* Một tài khoản người đưa thư
* Một máy chủ web hỗ trợ OAuth 2.0
* Một cơ sở dữ liệu để lưu trữ các tác vụ của bạn

### Tạo API

Để tạo API, chúng tôi sẽ sử dụng [Trình tạo API Postman] (https://www.postman.com/api-generator/).Công cụ này cho phép bạn tạo API RESTful chỉ bằng một vài cú nhấp chuột.

1. Mở Trình tạo API Postman và nhấp ** Tạo API **.
2. Nhập tên cho API của bạn và nhấp vào ** Tạo **.
3. Trình tạo API sẽ tạo API mới với một tập hợp các điểm cuối mẫu.
4. Nhấp vào ** Chỉnh sửa ** để mở trình chỉnh sửa API.
5. Trong tab ** điểm cuối **, thêm các điểm cuối sau:

`` `
Bài /nhiệm vụ
Nhận /Nhiệm vụ
`` `

Điểm cuối `Post /Tasks` sẽ được sử dụng để tạo một nhiệm vụ mới.Điểm cuối `get /nhiệm vụ sẽ được sử dụng để liệt kê tất cả các tác vụ.

6. Trong tab ** model **, thêm mô hình sau:

`` `
{
"ID": "Số nguyên",
"Tiêu đề": "Chuỗi",
"Mô tả": "Chuỗi",
"Duedate": "Ngày"
}
`` `

Trường `id` là mã định danh duy nhất cho tác vụ.Trường `Tiêu đề` là tiêu đề của nhiệm vụ.Trường `mô tả` là mô tả của tác vụ.Trường `Duedate` là ngày đáo hạn của nhiệm vụ.

7. Nhấp vào ** Lưu ** để lưu API của bạn.

### Cấu hình OAuth 2.0

Để định cấu hình OAuth 2.0 cho API của bạn, bạn sẽ cần tạo máy khách OAuth 2.0 mới.

1
2. Nhập tên cho dự án của bạn và nhấp vào ** Tạo **.
3. Nhấp vào ** API & Dịch vụ **> ** Màn hình đồng ý OAuth 2.0 **.
4. Nhấp vào ** Tạo thông tin đăng nhập **> ** ID máy khách OAuth **.
5. Chọn ** Ứng dụng web ** và nhấp ** Định cấu hình màn hình đồng ý **.
6. Nhập tên cho ứng dụng của bạn và nhấp vào ** Lưu **.
7. Sao chép ID máy khách ** ** và ** Bí mật máy khách ** giá trị.

### Tạo mã thông báo truy cập

Để tạo mã thông báo truy cập, bạn có thể sử dụng luồng ủy quyền OAuth 2.0 của Postman.

1. Mở Postman và tạo một yêu cầu mới.
2. Chọn tab ** ủy quyền ** và nhấp vào ** OAuth 2.0 **.
3. Nhập thông tin sau:

*** URL ủy quyền: ** https://accounts.google.com/o/oauth2/v2/auth
*** URL mã thông báo: ** https://accounts.google.com/o/oauth2/v2/token
*** ID máy khách: ** <Your_Client_ID>
*** Bí mật máy khách: ** <Your_Client_Secret>
*** Phạm vi: ** https://www.googleapis.com/auth/userinfo.email
*** Chuyển hướng URI: ** http: // localhost: 3000/gọi lại

4. Nhấp vào ** ủy quyền **.
5. Trong cửa sổ trình duyệt mở, nhấp vào ** cho phép ** cấp quyền truy cập người đưa thư vào tài khoản Google của bạn.
6. Cửa sổ trình duyệt sẽ đóng và Postman sẽ trả về phản hồi JSON với mã thông báo truy cập.

### Sử dụng mã thông báo truy cập

Để sử dụng mã thông báo truy cập để truy cập API của bạn, bạn có thể sử dụng tiêu đề sau:

`` `
Ủy quyền: Người mang <Com access_Token>
`` `

Ví dụ: yêu cầu sau đây sẽ liệt kê tất cả các tác vụ trong API của bạn:

`` `
Nhận https: // localhost: 3000/nhiệm vụ
Ủy quyền: Người mang <Com access_Token>
`` `
=======================================
## How to OAuth 2.0 Authorization with Postman

OAuth 2.0 is a popular authorization framework that allows users to grant third-party applications access to their protected resources without having to share their passwords. This is done by issuing an access token, which is a short-lived credential that grants the application limited access to the user's resources.

Postman is a popular API development tool that can be used to test and document APIs. It also includes a built-in OAuth 2.0 authorization flow, which makes it easy to integrate OAuth 2.0 into your API applications.

In this tutorial, we will show you how to use OAuth 2.0 authorization with Postman. We will create a simple API that allows users to create and list tasks. We will then use Postman to generate an access token and use it to access the API's protected resources.

### Prerequisites

To follow this tutorial, you will need the following:

* A Postman account
* A web server that supports OAuth 2.0
* A database to store your tasks

### Creating the API

To create the API, we will use the [Postman API Generator](https://www.postman.com/api-generator/). This tool allows you to create a RESTful API with just a few clicks.

1. Open the Postman API Generator and click **Create API**.
2. Enter a name for your API and click **Create**.
3. The API Generator will create a new API with a set of sample endpoints.
4. Click **Edit** to open the API editor.
5. In the **Endpoints** tab, add the following endpoints:

```
POST /tasks
GET /tasks
```

The `POST /tasks` endpoint will be used to create a new task. The `GET /tasks` endpoint will be used to list all of the tasks.

6. In the **Models** tab, add the following model:

```
{
"id": "integer",
"title": "string",
"description": "string",
"dueDate": "date"
}
```

The `id` field is the unique identifier for the task. The `title` field is the task's title. The `description` field is the task's description. The `dueDate` field is the task's due date.

7. Click **Save** to save your API.

### Configuring OAuth 2.0

To configure OAuth 2.0 for your API, you will need to create a new OAuth 2.0 client.

1. Open the [Google Developers Console](https://console.developers.google.com/) and click **Create Project**.
2. Enter a name for your project and click **Create**.
3. Click **APIs & Services** > **OAuth 2.0 consent screen**.
4. Click **Create Credentials** > **OAuth client ID**.
5. Select **Web application** and click **Configure consent screen**.
6. Enter a name for your application and click **Save**.
7. Copy the **Client ID** and **Client secret** values.

### Generating an access token

To generate an access token, you can use the Postman OAuth 2.0 authorization flow.

1. Open Postman and create a new request.
2. Select the **Authorization** tab and click **OAuth 2.0**.
3. Enter the following information:

* **Authorization URL:** https://accounts.google.com/o/oauth2/v2/auth
* **Token URL:** https://accounts.google.com/o/oauth2/v2/token
* **Client ID:** <YOUR_CLIENT_ID>
* **Client secret:** <YOUR_CLIENT_SECRET>
* **Scope:** https://www.googleapis.com/auth/userinfo.email
* **Redirect URI:** http://localhost:3000/callback

4. Click **Authorize**.
5. In the browser window that opens, click **Allow** to grant Postman access to your Google account.
6. The browser window will close and Postman will return a JSON response with an access token.

### Using the access token

To use the access token to access your API, you can use the following header:

```
Authorization: Bearer <ACCESS_TOKEN>
```

For example, the following request will list all of the tasks in your API:

```
GET https://localhost:3000/tasks
Authorization: Bearer <ACCESS_TOKEN>
```
 
Join ToolsKiemTrieuDoGroup
Back
Top
AdBlock Detected

We get it, advertisements are annoying!

Sure, ad-blocking software does a great job at blocking ads, but it also blocks useful features of our website. For the best site experience please disable your AdBlocker.

I've Disabled AdBlock