Home>

I want to post an article to a spreadsheet (googleappsscript) or WordPress

Error message
{"code": "rest_cannot_create", "message": "\ u3053 \ u306e \ u30e6 \ u30fc \ u30b6 \ u30fc \ u3068 \ u3057 \ u3066 \ u6295 \ u7a3f \ u3092 \ u7de8 \ u96c6 \ u3059 \ u308b \ u6a29 \ u9650 \ u304c \ u3042 \ u308a \ u307e \ u305b \ u3093 \ u3002 "," data ": {" status ": 401}}

Message after conversion at the following URL

{"code": "rest_cannot_create", "message": "You don't have permission to edit posts as this user.", "data": {"status": 401}}
Applicable source code The part of

// ****** is actually the correct one.

function POST_API () {
  var URL = 'http: //www.******/wp-json/wp/v2/posts'
  var username = '******'
  var pass = 'vq6y BTf7 Sv7D Eph8 C4Fe NbjB'
  var method = 'POST'
  var headers = {
    'Authorization': 'Basic' + Utilities.base64Encode (username + ':' + pass)
  };
  var body = {
    'title': 'title',
    'content': 'hogehoge',
    'status': 'publish'
  }
  var options = {
    'method': method,
    'headers': headers,
    'body': body,
    'muteHttpExceptions': true
  };

  var response = UrlFetchApp.fetch (URL, options);
  Logger.log (response)
}

Application Passwords has been activated
User name password has been issued
https://en.wordpress.org/plugins/application-passwords/

I tried to rewrite PHP⇒GAS on my own referring to the following site, but it didn't work.
https://it-engineer-lab.com/archives/312
http://en.wp-api.org/extending/javascript-client/

Addition:

I tried hitting the following command with reference to the above, but the error message was the same.
Is the ID password wrong? I tried to add a new user, but the result was the same.
>curl --header "Authorization: Basic ********" -X POST -d "title = From API" http: // ************/wp-json/wp/v2/posts

I wonder if the plug-in is affected, disable it all, or
The security-related things that seemed to be related were disabled, but there was no change.

Supplemental information (FW/tool version etc.)

Please provide more information here.

  • Answer # 1

    It may be unnecessary information because it was solved by e-mail,
    I think that POST from GAS to WP REST can be done with the following two corrections. (Success in our environment.)

    Please refer to the reference page below for details of GAS fetch.
    Class UrlFetchApp | Apps Script | Google Developers

    Delete space for "headers"

    Delete one space before Basic

    Before correction

    var headers = {
        'Authorization': 'Basic' + Utilities.base64Encode (username + ':' + pass)
      };

    After correction

    var headers = {
        'Authorization': 'Basic' + Utilities.base64Encode (username + ':' + pass)
      };
    Change option

    Change body → payload

    Before correction

    var body = {
        'title': 'title',
        'content': 'hogehoge',
        'status': 'publish'
      }
      var options = {
        'method': method,
        'headers': headers,
        'body': body,
        'muteHttpExceptions': true
      };

    After correction

    var payload = {
        'title': 'title',
        'content': 'hogehoge',
        'status': 'publish'
      }
      var options = {
        'method': method,
        'headers': headers,
        'payload': payload,
        'muteHttpExceptions': true
      };

  • Answer # 2

    There was a plugin to update WordPress by email

    I decided to implement something similar by sending mail from GAS.

    https://en.jetpack.com/support/post-by-email/

Related articles