WPAPI的软件授权学习认证
HTTPS认证
根据客户提供的key作为帐号,secret key作为密码,使用 HTTP Basic Auth,就像这样:
$ curl https://www.example.com/wc-api/v1/orders -u consumer_key:consumer_secret
如果你手工设置认证头部,必须先base64_encode一下:
$authorization = base64_encode( $consumer_key . ':' . $consumer_secret );
HTTP认证
HTTP Basic Auth不能使用纯HTTP作为键,因为它很可能会丢失,该API采用了 OAuth 1.0a “one-legged” authentication来避免机械隔膜计量泵这样的情况,这样,HTTP进行认证时,会产生包含了请求的一个签名,API收到请求后,会产生另外一个签名,并与请求的签名进行对比,如果两个签名相同,认证通过。如下图所示(下图截取自http://oauthbible.com/#oauth-10a-one-legged):
OAuth 1.0a “one-legged” authentication
OAuth 1.0a “one-legged” authentication
这个产生签名的过程并不难,但是要严格遵守文档说明(麻烦?费时?),所以,最好的方法是采用现成的库!
相关文档: The OAuth 1.0 Protocol RFC 5849
Parameters/参数
这个API的许多参数接受查询字符串参数,例如:.../orders?status=completed,也就是说,许多重要的参数只是机械隔膜计量泵过滤器参数,用于查询时间段(某时间段内的订单等)、关键字(用于搜索)、分页。
更多信息: REST API documentation
API索引
索引商店的公开信息是不需要认证的,这句话有点儿低级,但是值得说下。重要的数据是需要认证的,简单的示例.
示例库文件
WooCommerce REST API
https://github.com/kloon/WooCommerce-REST-API-Client-Library/issues/4
---------------------------------------------------------
客户端用例
<?php
/*
客户端使用示例
*/
error_reporting( E_ALL ); // 调试使用
ini_set( 'display_errors', 'On' ); // 调试使用
require_once "../class-wc-api-client.php"; // 自定义库文件
$consumer_key = 'ck_95ac118cf3d745ac3171d8e08c86846d'; // Consumer Key
$consumer_secret = 'cs_184233c882dbaf423ace8020bd373d02'; // Consumer Secret
$store_url = 'http://hao123.com/'; // 店铺首页链接 认证http://www.zol.com.cn/
$wc_api = new WC_API_Client( $consumer_key, $consumer_secret, $store_url );