1

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
  • 1
    What are the acceptable parameters in `option` according to official urlfetch documentation? – TheMaster Jun 25 '19 at 11:47
  • Please don't edit your question to invalidate the answer already provided. [Edit] to add additional information and Do not delete old information. – TheMaster Jun 25 '19 at 13:40

1 Answers1

0

Part 1 - payload

params object has a parameter called "payload" that should contain data you are going to send as a stringified JSON. Thus, instead of direct reference of the content, subject and type, you should do the following (btw, content type for UrlFEtchApp can be set via contentType parameter and the method via the corresponding parameter):

var option = {
  //other parameters here;
  method : 'post',
  contentType : 'application/json',
  payload : JSON.stringify( {
  content : '',
  subject : '',
  type    : ''
  } )
}

Part 2 - headers

Though it may seem arbitrary, not all of the parameters should be moved to top-level properties of the params object. There is a closed set of properties that can be set this way (see reference). Authorization should still be set as a header, thus:

var option = {
  //other parameters here;
  headers : {
    Authorization : 'Basic ' + yourAuth
  },
  method : 'post',
  contentType : 'application/json',
  payload : JSON.stringify( {
    content : '',
    subject : '',
    type    : ''
  } )
}

Useful links

  1. UrlFetchApp.fetch() reference;
  • Hi, @AndyFitzsimon! Please, clarify, what exactly you meant by your comment? (OK, got the update - let me know if it works for you - other than wrapping everything in payload, there shouldn't be any problems) – Oleg Valter is with Ukraine Jun 25 '19 at 12:13
  • Well There's some 'progress' . 401 request failed. At least something is happening. I'm guessing the authorization is now an issue not the option object – Andy Fitzsimon Jun 25 '19 at 12:20
  • function pushItGood() { var encode = Utilities.base64Encode('USER:PASS', Utilities.Charset.UTF_8); var url = "https://TESTDOMAIN.com/api/core/v3/places/XXXXXX/contents"; var option = { authorization: "Basic " + encode, contentType: "application/json", method: 'post', payload: JSON.stringify({ subject: 'testing', type: "document", content: { type: 'text/html', text: 'testinghere' }, }) } ........ – Andy Fitzsimon Jun 25 '19 at 12:21
  • Andy, please, update your question with the new structure of the code and I'll take a look (its hard to look through unformatted code) - if you are getting 401, then its the authorization that's causing issues now – Oleg Valter is with Ukraine Jun 25 '19 at 12:24
  • Thank you, it's updated in the question . The appropriate USER/PASS and URL's in my testing reflect what I use in my CURL commands – Andy Fitzsimon Jun 25 '19 at 12:29
  • Andy, that's easy, I'll update the answer in a second! – Oleg Valter is with Ukraine Jun 25 '19 at 12:30
  • That would be tremendously appreciated! – Andy Fitzsimon Jun 25 '19 at 12:33
  • Don't mention! I've updated the answer, with this everything should work like a charm. – Oleg Valter is with Ukraine Jun 25 '19 at 12:35
  • NP! If you are accustomed to how the requests are usually handled in JS, `UrlFetchApp` usage might be a bit confusing) – Oleg Valter is with Ukraine Jun 25 '19 at 12:42
  • I actually had a bounty open for this and got no responses. [All yours if you click answer and type anything as the solution](https://bountify.co/google-app-script-docs-document-to-jive-rest-api-document) – Andy Fitzsimon Jun 25 '19 at 12:43
  • Thanks, but leave it be - it's and open community here and we glad to help - just always accept the answer if it helped you as a small courtesy :) – Oleg Valter is with Ukraine Jun 25 '19 at 12:48
  • cool mate, done. you effectively just made a 50 USD donation to the Against Malaria Foundation. Thanks again for solving this on behalf of everyone who groks CURL but not Google's App Script apps – Andy Fitzsimon Jun 25 '19 at 13:08
  • Glad to help! Don't hesitate to ask the community if you encounter any trouble (though, always include the script sample / research effort) – Oleg Valter is with Ukraine Jun 25 '19 at 13:13