Curl is posting but Google AppScript is not with the same credentials.
Am trying to get google app script to post the current document as html content to a new Jive Document
// The following curl command works flawlessly
curl -u USERNAMEHERE:PASSWORDHERE -H "Content-Type: application/json" --data '{ "type": "document", "content": { "type": "text/html", "text":"<h1>HOORAY</h1> a Document is born"}, "subject": "TEST WORKED"}' https://MYJIVEURL.com/api/core/v3/places/XXXXXXXX/contents
// Apps Script is now throwing a 401 and failing
function pleaseWork() {
var encode = Utilities.base64Encode('USER:PASS', Utilities.Charset.UTF_8);
var docbody = DocumentApp.getActiveDocument().getBody();
var subject = DocumentApp.getActiveDocument().getName();
var url = "https://JIVEURL/api/core/v3/places/XXXXXX/contents";
var option = {
authorization: "Basic " + encode,
contentType: "application/json",
method: 'post',
payload: JSON.stringify({
subject: subject,
type: "document",
content: {
type: 'text/html',
text: docbody
},
})
}
var response = UrlFetchApp.fetch(url, option).getContentText()
}```
Theres no other errors to speak of in the AppScript editor. So I must be leaving something out. I just don't know what that is
Part 1 -
payloadparamsobject has a parameter called "payload" that should contain data you are going to send as a stringified JSON. Thus, instead of direct reference of thecontent,subjectandtype, you should do the following (btw, content type forUrlFEtchAppcan be set viacontentTypeparameter and themethodvia the corresponding parameter):Part 2 -
headersThough it may seem arbitrary, not all of the parameters should be moved to top-level properties of the
paramsobject. There is a closed set of properties that can be set this way (see reference). Authorization should still be set as aheader, thus:Useful links
UrlFetchApp.fetch()reference;