Wordpress - 如何使用REST API更新用户密码

时间:2017-03-24 08:42:11

标签: android wordpress rest api woocommerce

我正在使用 //This will return the build absolute path in ss variable StringSelection ss=new StringSelection(uploadfile().getAbsolutePath()+"\\"+"TestJpegFile.jpeg"); Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); //Call this function while setup your path public File uploadfile() { File datadir=new File("InputData\\images\\"); try { if(!datadir.exists()) { datadir.mkdir(); } }catch(Exception e) { e.printStackTrace(); } return datadir; } 以及wordpress用于我的网上商店,并使用woocommerce用于woocommerce REST API

我使用Android appWP REST API插件进行用户身份验证并通过rest api登录。

现在我在api下面使用更改密码

JWT Authentication for WP-API

低于错误

  

{“code”:“rest_cannot_edit”,“message”:“抱歉,您不被允许   编辑此用户。“,”data“:{”status“:401}}

我不知道为什么会出现此错误,因为在登录时进行了一次身份验证。任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:4)

创建您的自定义api

URL

https://yourdomain/api/change_password.php

参数

user_id:10
password:123456  //current password 
new_password:123456

在根目录中创建文件夹 api 并创建文件change_password.php

change_password.php

<?php
include '../wp-load.php';

$user_id = $_REQUEST['user_id'];
$user = get_user_by( 'id', $user_id );

$password = $_REQUEST['password'];
$new_password = $_REQUEST['new_password'];

if(empty($user_id)){
    $json = array('code'=>'0','msg'=>'Please enter user id');
    echo json_encode($json);
    exit;    
}
if(empty($password)){
    $json = array('code'=>'0','msg'=>'Please enter old password');
    echo json_encode($json);
    exit;    
}
if(empty($new_password)){
    $json = array('code'=>'0','msg'=>'Please enter new password');
    echo json_encode($json);
    exit;    
}
$hash = $user->data->user_pass;
$code = 500; $status = false;
if (wp_check_password( $password, $hash ) ){
    $msg = 'Password updated successfully';
    $code = 200; $status = true;
    wp_set_password($new_password , $user_id);
}else{
    $msg = 'Current password does not match.';
}




$json = array('code'=>$code,'status'=>$status,'msg'=>$msg);
echo json_encode($json);
exit;

?>

它对我来说100%有效

答案 1 :(得分:0)

您需要使用ajax调用传递会话令牌/承载/随机数。在这里,您已经获得了特定的文档:

https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/#cookie-authentication

答案 2 :(得分:0)

我有类似的问题。如果您已执行插件文档页面上提到的所有步骤,那么您用于获取令牌的帐户可能存在问题。

下面是我创建的视频,其中详细介绍了该插件的整个安装/设置过程。尝试按照我概述的步骤再次测试。

https://youtu.be/Mp7T7x1oxDk

答案 3 :(得分:0)

尝试通过添加以下几行来编辑.htaccess文件

RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]
SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

和您的wp-config.php通过添加

define('JWT_AUTH_SECRET_KEY', 'your-top-secret-key');

不要忘记在标头API调用中传递JWT_token,例如

*Authorization : 'Bearer ' + YOUR_JWT_TOKEN*