WP REST API (WP API) 是一個 WordPress 插件,其用途是為 WordPress 核心添加一個 JSON REST API , 以便於像移動應用之類的應用與 WordPress 進行交互。
WP-API 是可擴展的,並且具有齊備的文檔,如果你使用 WP REST API (WP API) ,你很可能會被授權問題所困擾。
WP
REST API (WP API)
能讓你創建、編輯獲取文章 (各種 WordPress 內置的文章類型的文章以及自定義類型的文章) 、創建、編輯和獲取用户等,因此,它在某些情形下需要認證
(授權),比如創建和編輯操作,就絕對需要授權才行,否則,處理申請會被 WordPress 拒絕。
據 WP REST API (WP API) 的認證 (授權) 文檔來看,認證方式有三種:cookie 、 oauth 和簡單認證。本文記錄如何實現自定義認證。
據 WordPress 官方開發記錄顯示:這個插件很可能會在 2015 年 4 月 22 日發佈的 WordPress 4.2 版本中加入到 WordPress 核心,那樣的話,授權方式可能會有所改變,但不會大變。
舉個簡單的例子: 某個用户通過手機拍了一張照片,想上傳到某個啓用了 WP REST API (WP API) 的 WordPress 企業網站源碼,那麼,就需要認證了吧,那麼,怎麼做呢?
下面將説一種用於此種情形的認證方式。
用自定義 filter hook
在該插件目錄 lib 下有個類文件 class-wp-json-server.php , 其中有這段兒:
-
/** -
* Check the authentication headers if supplied -
* -
* @return WP_Error|null WP_Error indicates unsuccessful login, null indicates successful or no authentication provided -
*/ -
public function check_authentication() {
-
/** -
* Pass an authentication error to the API -
* -
* This is used to pass a {@see WP_Error} from an authentication method -
* back to the API. -
* -
* Authentication methods should check first if they're being used, as -
* multiple authentication methods can be enabled on a site (cookies, -
* HTTP basic auth, OAuth). If the authentication method hooked in is -
* not actually being attempted, null should be returned to indicate -
* another authentication method should check instead. Similarly, -
* callbacks should ensure the value is `null` before checking for -
* errors. -
* -
* A {@see WP_Error} instance can be returned if an error occurs, and -
* this should match the format used by API methods internally (that is, -
* the `status` data should be used). A callback can return `true` to -
* indicate that the authentication method was used, and it succeeded. -
* -
* @param WP_Error|null|boolean WP_Error if authentication error, null if authentication method wasn't used, true if authentication succeeded -
*/ -
return apply_filters( 'json_authentication_errors', null );
-
}
基於上面的這個函數以及其被調用位置,我們可以加進去一個 hook,以確認認證是否成功:
-
/** -
* WP JSON API 認證檢查 -
* @param null -
* @return boolean 是否認證成功 -
* @author suifengtec coolwp.com -
*/ -
function coolwp_rest_api_auth_check( $result ){
-
-
if(
-
!isset($_GET['id'])
-
// ||!isset($_GET['app_key']) -
||!isset($_GET['app_token'])
-
||empty($_GET['id'])
-
// ||empty($_GET['app_key']) -
||empty($_GET['app_token'])
-
-
-
){
-
return false;
-
} -
-
//獲取從應用 GET 過來的用户 id 、 app_key 和 app_token, 當然了,你也可以只用一個去 app_key 和 app_token 中的任何一個去檢查 -
$user_id = (int)$_GET['id'];
-
// $app_key = sanitize_text_field($_GET['app_key']); -
$app_token = sanitize_text_field($_GET['app_token']);
-
-
//查詢 app_key 和 app_token, 當然了, 你也可以自定義一種算法, -
//$wp_key = get_user_meta( $user_id, 'app_key', true); -
$wp_token = get_user_meta( $user_id, 'app_token', true);
-
-
-
//將從應用客户端獲取到的值與數據庫存儲的值進行對比 -
if(
-
( $wp_token == $app_token )
-
// &&( $wp_key == $app_key ) -
-
){
-
-
return true;
-
} -
-
return false;
-
-
} -
add_filter('json_authentication_errors', 'coolwp_rest_api_auth_check');
結論
加入 rest api 的 WordPress 甚至可以讓你做一個在線支付網站,有了這組 api ,基於 WordPress 的原生安卓應用和 IOS 應用可以更好的與 WordPress 站點進行交互。