---
description: https://www.youtube.com/watch?v=pRFCmjiRNco&feature=emb_title
---

# \[AUTHENTICATE] WP REST API - Authenticate using OAuth2 (ok)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT7tC1FlNjLzlBwoPA%2F-MPT87pLQ4DFsMhAlDjq%2FScreenshot_8.jpg?alt=media\&token=71143a62-c46e-4b16-8ae5-64d197d603a6)

Ở [hocdekhangdinhminh.com](https://hocdekhangdinhminh.com/) cài plugin OAuth Server :)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT-J3vaZ2lWzcagvlH%2F-MPT-cMPVPbA3U25p0Bi%2FScreenshot_1.jpg?alt=media\&token=8d06a304-ad42-4bbe-b52d-117362076f00)

```
Client ID: NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K 
Client Secret: qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52
```

{% embed url="https://github.com/adoy/php-oauth2" %}

Lấy mã nguồn ở đây :)

{% embed url="https://github.com/adoy/PHP-OAuth2.git" %}

{% file src="https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT-J3vaZ2lWzcagvlH%2F-MPT0D_DLxxOaiTXm9-b%2FPHP-OAuth2-master.zip?alt=media&token=781bb779-bb32-4343-a11c-9db33d8700f9" %}

#### Ví dụ 1: Xác thực ở hanam.work

#### public\_html\test.php (hanam.work)

```
<?php
require 'vendor/autoload.php';
const CLIENT_ID              = 'NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K';
const CLIENT_SECRET          = 'qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52';
const REDIRECT_URI           = 'https://hanam.work/test.php';
const AUTHORIZATION_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/authorize';
const TOKEN_ENDPOINT         = 'https://hocdekhangdinhminh.com/oauth/token';
$client                      = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if (!isset($_GET['code'])) {
  $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
  header('Location: ' . $auth_url);
  die('Redirect');
} else {
  $params = array(
    'code'         => $_GET['code'],
    'redirect_uri' => REDIRECT_URI,
  );
  $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
  echo '<pre>';
  print_r($response);
  echo '</pre>';
}
?>
```

Truy cập đường dẫn này [https://hanam.work/test.php](https://hanam.work/test.php) nó tự chuyển về dạng [https://hanam.work/test.php?code=utmznu4sowxst6fdk2d7fpkkfml7wgwycp57jwjp\&iframe=break](https://hanam.work/test.php?code=utmznu4sowxst6fdk2d7fpkkfml7wgwycp57jwjp\&iframe=break)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT-J3vaZ2lWzcagvlH%2F-MPT1UaUfPJ5KjrRVCS0%2FScreenshot_2.jpg?alt=media\&token=a8984220-6479-4043-83b0-094ea80405b3)

Cấu trúc thư mục :)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT1XnGhj1Xr8Yk6HRn%2F-MPT2SxhMbfwpsOyOqY7%2FScreenshot_5.jpg?alt=media\&token=bcf3fadd-ebc2-468f-96f9-a9a1bcfc8de8)

#### Ví dụ 2: Xác thực ở hocdekhangdinhminh.com

public\_html\oauth\test.php (hocdekhangdinhminh.com)

```
<?php
require 'vendor/autoload.php';
const CLIENT_ID              = 'NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K';
const CLIENT_SECRET          = 'qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52';
const REDIRECT_URI           = 'https://hocdekhangdinhminh.com/oauth/test.php';
const AUTHORIZATION_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/authorize';
const TOKEN_ENDPOINT         = 'https://hocdekhangdinhminh.com/oauth/token';
$client                      = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if (!isset($_GET['code'])) {
  $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
  header('Location: ' . $auth_url);
  die('Redirect');
} else {
  $params = array(
    'code'         => $_GET['code'],
    'redirect_uri' => REDIRECT_URI,
  );
  $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
  echo '<pre>';
  print_r($response);
  echo '</pre>';
}
?>
```

Truy cập [https://hocdekhangdinhminh.com/oauth/test.php](https://hocdekhangdinhminh.com/oauth/test.php) nó tự chuyển về dạng [https://hocdekhangdinhminh.com/oauth/test.php?code=c13pzprgitytumfatkqv7wr3xggzx8zfmsuwz7ll\&iframe=break](https://hocdekhangdinhminh.com/oauth/test.php?code=c13pzprgitytumfatkqv7wr3xggzx8zfmsuwz7ll\&iframe=break)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT1XnGhj1Xr8Yk6HRn%2F-MPT21ydN7Hc9WIqrMVx%2FScreenshot_3.jpg?alt=media\&token=15a10b5b-1296-4ac8-9416-db18bc38a0e0)

Cấu trúc thư mục như sau để cho dễ phát triển

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT1XnGhj1Xr8Yk6HRn%2F-MPT2ISGgtdwF3pDt6bl%2FScreenshot_4.jpg?alt=media\&token=5989c418-4270-4165-b73c-27c7d41cc774)

#### Ví dụ 3: Hoàn thành ví dụ 1, ví dụ 2 sử dụng OAuth để lấy bài viết "revisions"

#### public\_html\test.php (hanam.work)

```
<?php
require 'vendor/autoload.php';
const CLIENT_ID              = 'NquBsEdmLk23ByRt3U5LUjLNX628aELPTsTTmm3K';
const CLIENT_SECRET          = 'qw69GVPAXnVcSH3uE0fStoYjNgI6lyvolTzzxn52';
const REDIRECT_URI           = 'https://hanam.work/test.php';
const AUTHORIZATION_ENDPOINT = 'https://hocdekhangdinhminh.com/oauth/authorize';
const TOKEN_ENDPOINT         = 'https://hocdekhangdinhminh.com/oauth/token';
$client                      = new OAuth2\Client(CLIENT_ID, CLIENT_SECRET);
if (!isset($_GET['code'])) {
  $auth_url = $client->getAuthenticationUrl(AUTHORIZATION_ENDPOINT, REDIRECT_URI);
  header('Location: ' . $auth_url);
  die('Redirect');
} else {
  $params = array(
    'code'         => $_GET['code'],
    'redirect_uri' => REDIRECT_URI,
  );
  $response = $client->getAccessToken(TOKEN_ENDPOINT, 'authorization_code', $params);
  $client->setAccessToken($response['result']['access_token']);
  $data = $client->fetch("https://hocdekhangdinhminh.com/wp-json/wp/v2/posts/1/revisions");
  echo '<pre>';
    var_export($data);
  echo '</pre>';
}
?>
```

Giờ thì lấy được giá trị như sau :)

![](https://2370029328-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LWPtKFbhw0XehxVuoiZ%2F-MPT2V1muPsBqqXa5tFo%2F-MPT7qIhmB542efc5MGs%2FScreenshot_7.jpg?alt=media\&token=fb5dba40-21f2-4ce9-8eac-4efcc4aaec45)

