node-wpapi
node-wpapi copied to clipboard
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?
Well, I don't know why, the issue is gone now... But still have another issue: now the response comes back with an error 400 rest_upload_no_data
. As I copied / pasted the example, I don't see what I'm doing wrong.
Hello! Glad to hear the initial error went away, although unfortunately I am not certain what could have been causing it.
As for the rest_upload_no_data
issue, I'd suggest logging out the value of uri
to understand what is being sent to the API in the initial file request. I have not attempted to integrate this with React Native, so it's possible that the method of accessing and passing through a file's information differs from node's or the browser's.
Hi, thanks for your reply.
The value of URI is a string like file://Users/enguerran/Library/Developer/CoreSimulator/Devices/6FD23363-521D-4DF8-85BA-3E9160758B73/data/Containers/Data/Application/D84B9177-EB10-486A-A587-14AC04B52709/Documents/7E3741EA-81F0-4A5E-91D4-DEC48069F8F5.jpg
as I'm testing on iOs simulator.
As the docs says a String describing an image file path, e.g. '/path/to/the/image.jpg'
, I also tried removing file:/
, with no success (still getting rest_upload_no_data
error).
Just to be sure, I tried using posts()
method, which works well.
My best guess is that in the React Native environment, there's some other piece of file access that we'd need to pass in lieu of that file: string... I unfortunately can't commit to building a proof-of-concept right now, but I'd recommend searching for examples of React Native image uploads to see what value they use to pass in to fetch
or whatever upload library they're using.
If you make any progress that suggests a way in which our documentation can be improved please comment here and we'll get it up on the docs site as soon as we can!
I'm using react-native-image-picker right now. Following the docs (https://github.com/react-community/react-native-image-picker#the-response-object), I can retrieve the image path, some urls, base64 data, etc.
I'm trying getting image data as base64 and make a new buffer from it, passing it to wp.media().file(imageDataAsBase64)
So, here's the result, I changed the original uri string to a buffer containing the base64 image data:
function base64DataToBuffer(dataString) {
response = new Buffer(dataString, 'base64');
return response;
}
imageBase64Data = base64DataToBuffer(imageBase64Data);
return wp.media()
.file( imageBase64Data )
...
This leads to an error (or something like an error) :
- when using
wp.media().file(imageBase64Data)
,imageBase64Data
is aUint8Array
variable - I get this message in the console:
TypeError: One of the sources for assign has an enumerable key on the prototype chain. Are you trying to assign a prototype property? We don't allow it, as this is an edge case that we do not support. This error is a performance optimization and not spec compliant.
As I'm not familiar with this (base64, buffers), I'm not sure what's going on there.
Assuming I fixed the error I got earlier with the following:
response = new Buffer(dataString).toString('base64');
I still got the same error: 400, rest_upload_no_data
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);
})
@anesumuz That seems obvious, but did you check that username / password are correct and registered on your WP install?
If you need more help on this, open your own issue please.
Before sending @anesumuz away, just wanted to point out again you need to use the Basic-Auth wordpress plugin to make this work.
See this quote from above:
And well, I used the WP Basic Auth handler and enabled it: https://github.com/WP-API/Basic-Auth
It's not available in the plugin repo so you need to download it here from Github.
There are many authorization solutions located within this thread
https://github.com/WP-API/Basic-Auth/issues/35
So, its an issue with apache headers / CGI / etc preventing the Basic Auth headers from getting passed along to the wordpress / wordpress plugin. If you visit the issues thread on WP-API, the htaccess solutions aren't really working for everyone. Here's what I did to get it working:
-
Installed this alternate basic auth plugin: https://github.com/eventespresso/Basic-Auth
-
go to
(nodemodules folder)/wpapi/lib/http-transport.js
and add this to the top of theinvokeAndPromisfy
function:
if(request.header.Authorization) {
request = request.query({'_authorization': request.header.Authorization })
};
it is a hacky fix, but basically, its passing the auth information as a request variable instead of as a header. its working for me for now, so maybe thats helpful for anyone that is wanting to hack around on this without messing with apache too much or before people that know alot more about it can figure out the basic auth situation. A lot of people seem to be having this problem :-/
@datesss Your method not sounds good, but really save my life, thanks!
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.
@kadamwhite is this dead?
@braco unfortunately, pretty much. I do not have bandwidth to maintain it right now, and we never completed an earlier attempt at migrating to typescript. If anybody else wants to maintain it, as long as the API stays similar, I would be happy to give commit and review an upcoming release.
@kadamwhite totally understand, thank you. Maybe consider putting that in the readme with a link to some alternatives like
https://github.com/dkress59/wordpress-api-client
? It takes a lot of time to discover the dead ends like this.