Help

The Community will be temporarily unavailable starting on Friday February 28. We’ll be back as soon as we can! To learn more, check out our Announcements blog post.

How to use Utilitites in airtable script

Topic Labels: API Scripting
Solved
Jump to Solution
3318 3
cancel
Showing results for 
Search instead for 
Did you mean: 
saadat
4 - Data Explorer
4 - Data Explorer
 var authHeader = "Basic " + Utilities.base64Encode(wordpressUsername + ":" + wordpressPassword);
 
I am getting error at "Utilities"  saying"Cannot find name 'Utilities'"  . I need to send post to wordpress using it's rest api and Utilities is required for it's authorization .
 
Kindly help me , any information  regarding this will be great. Thankyou
1 Solution

Accepted Solutions
Stephen_Orr1
10 - Mercury
10 - Mercury

It looks like you may be trying to use a Utilities object from some external library. Try btoa() instead
https://developer.mozilla.org/en-US/docs/Web/API/btoa

See Solution in Thread

3 Replies 3
Stephen_Orr1
10 - Mercury
10 - Mercury

It looks like you may be trying to use a Utilities object from some external library. Try btoa() instead
https://developer.mozilla.org/en-US/docs/Web/API/btoa

Thanks , I already tried and it worked but now as I am using wordpress rest api and tring to Post the blog getting 401 error , as code is all correct donot know what is reason , credentials and endpoints etc all are correct because this was working fine in google app script.

getting this response "

  1. {type: "basic", url: "https://philodendroncare.com/wp-json/wp/v2/posts", status: 401, statusText: "Unauthorized", ok: false…}
    1.  type: "basic"
    2.  status: 401
    3.  statusText: "Unauthorized"
    4.  ok: false
    5.  headers: Object
    6.  redirected: false"

 

async  function createBlogPost(title, content, status) {
  var wordpressUrl = "";
  var wordpressUsername = "";
  var wordpressPassword = "";
  
  var authHeader = 'Basic ' + btoa(wordpressUsername + ':' + wordpressPassword);
  
  // Create the blog post payload
  var postPayload = {
    title: title,
    content: content,
    status: status
  };

  // Convert the payload to JSON
  var payloadJson = JSON.stringify(postPayload);

  // Make an HTTP POST request to create the blog post
  var url = wordpressUrl + "/wp/v2/posts";
  
  var options = {
    method: "post",
    headers: {
      'Authorization': authHeader,
      'Content-Type': 'application/json'
    },
    body: payloadJson
  };

  await remoteFetchAsync(url, options)
    .then(response => {
        console.log(response)
      if (response.ok) {
        return response.json();
      } else {
        throw new Error("Error creating blog post. Status code: " + response.status);
      }
    })
    .then(data => {
      var postId = data.id;
      console.log("Blog post created with ID: " + postId);
    })
    .catch(error => {
      console.error(error.message);
    });
}

// Example usage
var title = "New Blog Post";
var content = "This is the content of the blog post.";
var status = "publish";

 await createBlogPost(title, content, status);

 

 

btoa() has some nuances that may be returning the wrong auth header. It accepts a “string” where each character represents an 8-bit byte – if you pass a string containing characters that can’t be represented in 8 bits, it will probably break

also see https://stackoverflow.com/a/29167148 for more details/solutions