May 29, 2023 11:40 AM
Solved! Go to Solution.
May 29, 2023 02:52 PM
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
May 29, 2023 02:52 PM
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
May 29, 2023 02:59 PM
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 "
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);
May 29, 2023 03:28 PM
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