node-wpapi: Error "rest_cannot_create", status 401

Hi,

I’m making an app with React Native, and I use this module to handle WP API requests. I’m just messing around right now, but I keep having an error: “rest_cannot_create”, with status 401 and message saying that I don’t have permission to perform my request.

Here’s how I perform the basic auth:

const wp = new WPAPI({
  endpoint: 'https://www.my-url.com/wp-json',
  username: 'username',
  password: 'passphrase',
  auth: true
});

And my request looks like this:

wp.media()
        .file( uri )
        .create({
            title: 'My awesome image',
            alt_text: 'an image of something awesome',
            caption: 'This is the caption text',
            description: 'More explanatory information'
        })
        .then(function( response ) {
            console.log(response); 
            var newImageId = response.id;
            return wp.media().id( newImageId ).update({
                post: userId
            });
        })
        .then(function( response ) {
            dispatch(setNewImageAvatar(response));
        })
        .catch(function( err ) {
          console.log(err);
        });

And well, I used the WP Basic Auth handler and enabled it: https://github.com/WP-API/Basic-Auth

What do I miss?

About this issue

Most upvoted comments

I have the same issue , my code is as below . I get the { code: 'rest_cannot_create',message: 'Sorry, you are not allowed to create posts as this user.',data: { status: 401 } } error. How can I resolve this, and is there any extra config needed on the WP dashboard side?

var WPAPI = require('wpapi');
var wp = new WPAPI({
    endpoint: 'http://my-url.com/wp-json',
    username: 'username',
    password: ' password',
    auth: true
});


wp.posts().create({
    // "title" and "content" are the only required properties
    title: 'Your Post Title',
    content: 'Your post content',
    // Post will be created as a draft by default if a specific "status"
    // is not specified
    status: 'publish'
}).then(function(response) {
    // "response" will hold all properties of your newly-created post,
    // including the unique `id` the post was assigned on creation
    console.log(response.id);
}).catch(function(err) {
    console.log(err);
})

If we introduce a generic way to modify outgoing requests (see #324) that will permit adding that query parameter without modifying the core library code. I will update this issue once we’ve implemented an option for that behavior.