Search code examples
nativescriptangular2-nativescriptwordpress-rest-api

How to properly set Authorization to consume Wordpress Rest API (Oauth1) with Nativescript[2.5.4]?


I'm trying to consume the WP Rest API with Nativescript.

  1. The WP API and the Authorization with OAUTH1 is already well setup and tested with POSTMAN.
  2. Nativescript Login function to consume rest is already setup too and work without OAUTH.

Now I'm trying to Login with OAUTH here the code :

     authorizationString = 'OAuth  oauth_consumer_key="T2yXcbN28Ufj",
      oauth_token="AmEVr5oSNmbKyZKccFjtmnSk",
      oauth_signature_method="HMAC-SHA1",
      oauth_timestamp="1492267438",
      oauth_nonce="rGFJG2",
      oauth_version="1.0",
      oauth_signature="Ru%2BaSvsZn2liesd2ENy8GeNsdHY%3D"';

     login(user: User){
      console.log("Try to login User : " + user.email);
      let headers = new Headers({"Authorization": authorizationString});
      let body = JSON.stringify({
        username: user.username,
        email: user.email,
        password: user.password
      });
      headers.append("Content-Type", this.contentType);

      return this.http.post(
        this.api,
        body,
        {headers: headers}
      )
      .map( response => response.json() )
      .do(data => {
        //Do work!!
      })
      .catch(this.handleErrors);
   }

But i got an error, this error means the autorization is not well formatted or sent :

"data": {
    "code": "rest_cannot_access",
    "message": "Only authenticated users can access the REST API.",
    "data": {
        "status": 401
    }
}

How to properly use oauth1 with Nativescript?


Solution

  • I just switched to OAUTH2 by using a opensource plugin :

    https://github.com/wlcdesigns/WP-OAuth2-Server-Client

    and it's more easy to use with the authorization : Bearer