首页 > 解决方案 > React Native App with WordPress REST API - 不允许以该用户的身份创建帖子

问题描述

我正在使用 REST API 使用简单的Wordpress 后端在React Native中开发一个应用程序,在使用方法之前我没有任何问题。GET

现在我需要使用该POST方法创建一篇博客文章,但仍然出现错误...

我有的:

  1. 带有激活的基本身份验证处理程序插件的 Wordpress -> https://github.com/WP-API/Basic-Auth
  2. 使用 WPAPI 客户端反应本机项目 - > https://github.com/WP-API/node-wpapi

我的反应代码:

const WPAPI = require( 'wpapi' );
let apiPromise = WPAPI.discover( 'https://myweb.com/wp-json' ).then(function( site ) {
return site.auth({
    username: 'admin',
    password: 'password'
  }); 
});
apiPromise.then(function( site ) {
    site.posts().create({
      title: 'Your Post Title',
      content: 'Your post content',
      status: 'publish'
    }).then(function( response ) {
        console.log( response );
    })
    .catch(function(err){
      console.log(err);
    });
})

并总是得到这个答案:

"code": "rest_cannot_create",
"status": 401,
"message": "Sorry, you are not allowed to create posts as this user."

标签: wordpressreactjsapireact-nativewp-api

解决方案


推荐阅读