最近经历了一个类似的问题,并学会了解决它,我想我应该分享一下我的学习经验。
我们了解到,任何人都可以通过status=publish
. 如果出现以下或类似错误,则应开始考虑身份验证或权限方面的问题:
stdClass Object
(
[code] => rest_invalid_param
[message] => Invalid parameter(s): status
[data] => stdClass Object
(
[status] => 400
[params] => stdClass Object
(
[status] => Status is forbidden.
)
[details] => stdClass Object
(
[status] => stdClass Object
(
[code] => rest_forbidden_status
[message] => Status is forbidden.
[data] => stdClass Object
(
[status] => 401
)
)
)
)
)
。。。或
stdClass Object
(
[code] => jwt_auth_invalid_token
[message] => Wrong number of segments
[data] => stdClass Object
(
[status] => 403
)
)
。。。如果您是以管理员身份登录的,那么您应该有查看草稿帖子的权限。因此,您的API调用可能出于某种原因没有进行身份验证。
我测试的第一件事是代币。我碰巧在用JWT Authentication for WP-API 插件,其实很容易实现。安装/激活JWT Auth后,转到要分配使用API调用的用户,在用户配置文件的底部,您将看到:
为应用程序命名,添加新的,然后会弹出一个新的应用程序密码。它将如下所示:
复制整个密码,保留空格就可以了。
我使用的是PHP和cURL,因此我的get-token代码如下所示:
private function getAuthHeader(){
$JWTtoken = json_decode($this->getJWTToken());
$token = $JWTtoken->token;
$header = array(
"Content-type: application/json",
"Authorization: Bearer " . $token
);
return $header;
}
public function getJWTToken(){
/*
Request: POST http://basic/wp-json/api/v1/token
Body:
username = <wordpress username>
password = <wordpress password>
*/
$url = $this->baseurl . "jwt-auth/v1/token";
$params = array(
\'requesttype\' => \'POST\',
\'url\' => $url,
\'post\' => array(\'username\' => \'admin\', \'password\' => \'esmA UJom vxAG LFKU q8oN DSGK\')
);
$ch = curl_init();
CURL_SETOPT($ch, CURLOPT_RETURNTRANSFER, 1);
CURL_SETOPT($ch, CURLOPT_URL, $params[\'url\']);
CURL_SETOPT($ch, CURLOPT_POST, TRUE);
CURL_SETOPT($ch, CURLOPT_POSTFIELDS, http_build_query($params[\'post\']));
CURL_SETOPT($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
。。。您将在$params中看到我设置用户名/密码的方式。用户名为;管理员“;,因为这就是我为其设置新应用程序密码的用户,密码是
esmA UJom vxAG LFKU q8oN DSGK
. 注意,我没有使用管理员的WordPress密码。
您的头成为一个数组,它应该如下所示,其中包括一个loooong标记字符串:
Array ( [0] => Content-type: application/json [1] => Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9iYXNpYyIsImlhdCI6MTYyMzI2NjUyNSwibmJmIjoxNjIzMjY2NTI1LCJleHAiOjE2MjM4NzEzMjUsImRhdGEiOnsidXNlciI6eyJpZCI6IjEifX19.YX1UvJ5nlbG2MIlheI2NzTTzaQKBZ8I9WQOr70CE1Tk )
最后,使用cURL发出请求的函数如下所示:
public function getResponse($params){
$auth_header = $this->getAuthHeader();
$ch = curl_init();
CURL_SETOPT($ch, CURLOPT_RETURNTRANSFER, 1);
CURL_SETOPT($ch, CURLOPT_URL, $params[\'url\']);
if( isset($params[\'requesttype\']) && $params[\'requesttype\'] == "POST" ){ //additional code to detect whether I\'m making a GET, POST, or PUT request.
CURL_SETOPT($ch, CURLOPT_POST, TRUE);
}
else if( isset($params[\'requesttype\']) && $params[\'requesttype\'] == "PUT"){
CURL_SETOPT($ch, CURLOPT_CUSTOMREQUEST, "PUT");
}
if(isset($params[\'post\'])){
CURL_SETOPT($ch, CURLOPT_POSTFIELDS, http_build_query($params[\'post\']));
}
CURL_SETOPT($ch, CURLOPT_SSL_VERIFYPEER, false);
CURL_SETOPT($ch, CURLOPT_HTTPHEADER, $auth_header);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
请注意,在执行cURL之前,我希望通过调用getAuthHeader函数来获取带有新标记的标头:
$auth_header = $this->getAuthHeader();
请注意,我必须将包含新令牌的标头传递给web服务:
CURL_SETOPT($ch, CURLOPT_HTTPHEADER, $auth_header);
除非有任何拼写错误,这一直是我的问题,否则应该返回状态为草稿的帖子:
public function getPosts($params){
// I passed $params[\'status\'] = "draft";
$url = $this->baseurl . "wp/v2/posts/?status=" . $params[\'status\'] . "";
$data = array(
\'requesttype\' => \'GET\',
\'url\' => $url
);
return $this->getResponse($data);
}