question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi,

I’m testing the WP Node API and I’m trying to create a post, however I get this error:

{ "code": "rest_cannot_create", "message": "Sorry, you are not allowed to create new posts.", "data": { "status": 401 } }

I’m authenticating with my login for wordpress and testing it on localhost. This is the code:

var WPAPI = require( 'wpapi' );

var wp = new WPAPI({
    endpoint: 'http://localhost/WordPressAPI/wordpress/wp-json',

    username: 'username',
    password: '--!',
    auth: true
})

var post = 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 );
});

Running this code in NodeJS doesn’t do anything either. Does someone know what I’m doing wrong? Thanks!

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
kadamwhitecommented, Sep 23, 2016

Hello @marcianosr and thanks for using the WordPress REST API (and this library!) May I ask whether you’re running the Basic Auth plugin on your site? That plugin must be installed and active for username/password authentication to succeed.

As a side note, the “auth: true” should be unnecessary, but would not contribute to the error you are seeing.

0reactions
kadamwhitecommented, Sep 24, 2016

@marcianosr my pleasure, I’m glad to hear you got things working! Thanks for the update and let us know if you run into anything else!

Read more comments on GitHub >

github_iconTop Results From Across the Web

401 Unauthorized - HTTP - MDN Web Docs
The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed ...
Read more >
How to Quickly Fix the 401 Unauthorized Error (5 Methods)
The 401 (Unauthorized) status code indicates that the request has not been applied because it lacks valid authentication credentials for the ...
Read more >
401 Error: 5 Ways to Troubleshoot and Fix It - Hostinger
The 401 Unauthorized error indicates that the server's request was not verified because it lacks valid authentication credentials for the target ...
Read more >
How to Fix a 401 Unauthorized Error? - GeeksforGeeks
The 401 Unauthorized Error is an HTTP status code error that represented the request sent by the client to the server that lacks...
Read more >
How to Fix a 401 Unauthorized Error - Lifewire
The 401 Unauthorized error is an HTTP status code that means the page you were trying to access cannot be loaded until you...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found