Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy WordPress REST API cannot_create JavaScript

    Hello.
    am currently trying to create a WordPress article from one WP site to another. For that I use OAuth 1.0 and the Disable Rest API plugin (both are used for authentication). I use JavaScript to link and I also use POSTMAN to do my tests.

    A POST request with postman will pass without worries or from my site that sends the information, only the GET requests pass, otherwise, the site that receives the information denies me access this way:

    Code:
    {
    "code": "rest_cannot_create",
    "message": "Sorry, you are not allowed to create new posts.",
    "data": {
    "status": 401
    }
    }
    I do not understand why I can execute a GET request to read the content but that I can not create content in POST.

    Could you help me ? I also give you the JavaScript code that I already did. I use Create-React-App. Thanks very much.

    Code:
    import oauthSignature from 'oauth-signature';
    
    /**
    * PARAMS
    */
    		  
    		  function genNonce() {
    			    const charset = 'ABCDEFGHIJKLMNOPQRSTUVXYZabcdefghijklmnopqrstuvwxyz'
    			    const result = [];
    			    window.crypto.getRandomValues(new Uint8Array(11)).forEach(c =>
    			        result.push(charset[c % charset.length]));
    			    return result.join('');
    			}
    		  
    		  var oauth_consumer_key = "FXdnpsRLUIkb";
    		  var oauth_token = "ajJxPGcMvB3NVBwQW7d2i1nN";
    		  var oauth_signature_method = "HMAC-SHA1";
    		  var oauth_timestamp = Math.floor(new Date().getTime()/1000);
    		  var oauth_nonce = genNonce();
    		  var oauth_version = "1.0";
    		  
    		  /**
    		   * SIGNATURE
    		   */
    		  
    		  var httpMethod = 'GET',
    		    url = 'https://mylink.pw/wp-json/',
    		    parameters = {
    		        oauth_consumer_key : 'FXdnpsRLUIkb',
    		        oauth_token : 'ajJxPGcMvB3NVBwQW7d2i1nN',
    		        oauth_nonce : oauth_nonce,
    		        oauth_timestamp : oauth_timestamp,
    		        oauth_signature_method : 'HMAC-SHA1',
    		        oauth_version : '1.0',
    		    },
    		    consumerSecret = 'q2bxo5hu6rYtcdWrv1hRDyY3dQA3Itsbn6foAtz1mOFgYM7b',
    		    tokenSecret = 'bcZ8kgL7EDekJm1ogUctm89My7w3K1bZxIYqCZAdoEwHbIfE',
    		    signature = oauthSignature.generate(httpMethod, url, parameters, consumerSecret, tokenSecret,
    		        { encodeSignature: false});
    		  
    		  var oauth_signature = signature;
    		  
    		  
    		  var connect_params = "&oauth_consumer_key=" + oauth_consumer_key + "&oauth_token=" + oauth_token + "&oauth_signature_method=" + oauth_signature_method + "&oauth_timestamp=" + oauth_timestamp + "&oauth_nonce=" + oauth_nonce + "&oauth_version=" + oauth_version + "&oauth_signature=" + oauth_signature + "&context=edit";
    		  
    		  
    
    		  /**
    		   * ENVOI
    		   */
    		  var data = "title=TITLE&content=CONTENT&excerpt=EXCERPT&status=publish" + connect_params;
    
    		  var xhr = new XMLHttpRequest();
    		  xhr.withCredentials = true;
    
    		  xhr.addEventListener("readystatechange", function () {
    		    if (this.readyState === 4) {
    		      console.log(this.responseText);
    		    }
    		  });
    		  
    		  xhr.open("POST", "https://mylink.pw/wp-json/wp/v2/posts/");
    		  xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    		  xhr.send(data);

  2. #2
    Master Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    5,842
    Thanks
    26
    Thanked 609 Times in 602 Posts
    I do not understand why I can execute a GET request to read the content but that I can not create content in POST.
    Maybe you don't have the permission for that?

    PS. you should change your secret credentials after posting them here publicly
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •