0

I need create user on Wordpress. I use WP_REST_API - this is default API for WP. You can look at it "YOU_SITE/wp-json/" I have ionic3 project and have function.

onSubmit(values){
    this.http.post(Config.WORDPRESS_URL + 'wp-json/jwt-auth/v1/token',{
      username: 'admin',
      password: 'pass'
    })
    .subscribe(
      res => {
        let token = res.json().token;
          let header : Headers = new Headers();
          header.append('Authorization','Basic ' + token);
        this.http.post(Config.WORDPRESS_REST_API_URL + 'users?token=' + res.json().token,{
          username: values.username,
          name: values.displayName,
          email: values.email,
          password: values.password,
        },header)
        .subscribe(
          result => {
            console.log(result.json());
          },
          error => {
            console.log(error.json());
          }
        )
      },
      err => {
        console.log(err);
      }
    )
  }

But I always get error:

code: "rest_cannot_create_user",
message: "Sorry, but you can't create new user"
status: 401

admin:pass - this is admin on site and has a role admin. Also I added to .htaccess

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule ^(.*) - [E=HTTP_AUTHORIZATION:%1]

Please help me to find a mistake

6
  • Have you installed the Basic Authentication plugin of the documentation ? developer.wordpress.org/rest-api/using-the-rest-api/… Commented Dec 9, 2017 at 11:23
  • Yes, I installed JWT Authentication for WP REST API Commented Dec 9, 2017 at 11:28
  • to use the token, try with "Bearer" instead of "basic" header.append('Authorization','Bearer ' + token); Commented Dec 9, 2017 at 11:55
  • doesn't work :( Commented Dec 9, 2017 at 13:40
  • Are you sure that login return a JWT? Try to log it. Then I noticed that you send the token via Authorization header and via query param. Try to exclude header and send only via query string and vice-versa. Commented Dec 9, 2017 at 20:16

1 Answer 1

1

I found my issue and solution. I delete param TOKEN from URL and create options header

let header = new Headers({"Authorization": "Bearer "+token});
let options = new RequestOptions({headers: header});

I have next request

this.http.post(Config.REGISTER, {
            username: username,
            name: displayName,
            email: email,
            password: password,
            nonce: nonce
        }, options)

it is work for me.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.