I'm trying to fetch preview data through WP Rest API using a url such as /wp-json/wp/v2/pages/1140/revisions/1653.

An extra header is also included: 'X-WP-Nonce': <nonce> - nonce comes from WP.

Why am I getting 401 unauth error? Thanks

{ code: 'rest_cannot_read',
2020-07-21T11:45:35.930292+00:00 app[web.1]:   message: 'Sorry, you are not allowed to view revisions of this post.',
2020-07-21T11:45:35.930293+00:00 app[web.1]:   data: { status: 401 } }
  • 1
    Seems like the user that Wordpress thinks you are when accessing the API doesn't have permission for that resource. Have you looked into how you login for the API? E.g. some content here cloudways.com/blog/…
    – mozboz
    Jul 21 '20 at 12:23
  • You're right! The API token was kind of expired (deleted). Did not even even need nonce. Thanks!
    – haxpanel
    Jul 21 '20 at 13:00
  • 1
    @haxpanel can you post that as an answer in case someone else has this issue? I can't upvote your answer and give you rep if it's just a comment :)
    – Tom J Nowell
    Jul 21 '20 at 14:02

The solution was to generate and use an API token.

  1. in WP admin, go to user and generate the key
  2. take that key and glue it with the username it is generated with in this format: username:key
  3. base64 encode that string
  4. put that base64 value into the header: 'Authorization': 'Basic <base64 token here>'

In my case, the key was deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.