搜索
Hi~登录注册
查看: 1457|回复: 0

WordPress:自定义WP REST API (WP API)授权 | WordPress教程

[复制链接]

1892

主题

1899

帖子

6406

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6406
发表于 2015-4-13 13:53:54 | 显示全部楼层 |阅读模式
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 站点进行交互。
公众微信:idc5ahl
公众QQ:吾爱互联
关注公众微信,公众QQ每天领现金卡密
卡密介绍(http://www.5ahl.com/thread-2182-1-1.html
回复

使用道具 举报

游客
回复
您需要登录后才可以回帖 登录 | 点我注册

快速回复 返回顶部 返回列表