質問

I've tried all the combinations I can think of to make this work. What do I need to send to the New Basecamp API as a POST field to have it accept my POST data?

My code is:

function getFilterBy() {
    var url = "https://basecamp.com/****/api/v1/projects/*****.json";
    var payload =  {"name" : "myapp", "description" : "no desc mls"};
    var opt = {
               "contentType" : "application/json",
               "method" : "POST",
               "headers":{ "User-Agent": "myapp (user@somewhere.com)",
                           "Authorization" :"Basic " + Utilities.base64Encode("user" + ":" + "pass")},
               "validateHttpsCertificates" :false,
               "muteHttpExceptions" : true,
               "payload" : payload
               };
    var response = UrlFetchApp.fetch(url, opt);
    var text = response.getContentText();
    Logger.log(text);
    }

My Error is:

lexical error: invalid char in json text.
                                   description=no+desc+mls&name=Ho
                 (right here) ------^

I have tried to Utilities.jsonStringify, but no luck. I know its a noob error, but I just can't figure it out. If there is a place you are aware of where I can get this info too that would be phenomenal.

Thanks!

UPDATE 1

function getFilterBy() {
       var url = "https://basecamp.com/****/api/v1/projects/****.json";
       var payload =  {name : 'myApp Change'};
       var opt = {
                   "contentType" : "application/json",
                   "method" : "POST",
                   "headers":{ "User-Agent": "myApp (user@ex.com)",
                   "Authorization" :"Basic " + Utilities.base64Encode("user" + ":" + "pass")},
                   "validateHttpsCertificates" :false,
                   "muteHttpExceptions" : true,
                   "payload" : Utilities.jsonStringify(payload)
                    };
        var response = UrlFetchApp.fetch(url, opt);
        var text = response.getContentText();
        Logger.log(text);
        }

Yields Err:

<body>
    <div class="dialog">
     <div class="innercol">
       <h1>Hmm, that isn&rsquo;t right</h1>
       <h2>You may have typed the URL incorrectly.</h2>
       <p>Check to make sure you&rsquo;ve got the spelling, capitalization, etc. exactly right.</p>
     </div>
    </div>
    <p id="back" class="back">
      <a href="javascript:window.history.back();">&larr; Back to previous page</a>
    </p>
    <script type="text/javascript">
        if (window.history.length <= 1) document.getElementById("back").style.display = "none";
    </script>
    </body>

役に立ちましたか?

解決

Are you sure you tried with JSON.stringify correctly? This example here correctly creates a project for me in Basecamp with the specifified project name/description in the POST payload.

Note, I wrote this quickly to not include the UserAgent - you'll want to put that back in.

function createProject() { 
  var user = 'USERNAME';
  var password = 'PASSWORD'
  var accoundId = 'ACCOUNTID#';

  var url = 'https://basecamp.com/'+accoundId+'/api/v1/projects.json';  
  var payload =  {name : 'new project', description : 'my project description'};
  var opt = {
    contentType : 'application/json',
    method : 'post',
    headers:{Authorization :"Basic " + Utilities.base64Encode(user + ':' + password)},
    validateHttpsCertificates :false,
    muteHttpExceptions : true,
    payload : JSON.stringify(payload)
  };

  var response = UrlFetchApp.fetch(url, opt);
  var text = response.getContentText();
  Logger.log(text);
}

In order to update a project, you have to use the put verb instead of post as documented here

Here is a working sample -

function updateProject() { 
  var projectId = '2413370';
  var url = 'https://basecamp.com/'+accoundId+'/api/v1/projects/'+projectId+'.json';  
  var payload =  {name : 'new project', description : 'my new new project description'};
  var opt = {
    contentType : 'application/json',
    method : 'put',
    headers:{Authorization :"Basic " + Utilities.base64Encode(user + ':' + password)},
    validateHttpsCertificates :false,
    muteHttpExceptions : true,
    payload : JSON.stringify(payload)
  };

  var response = UrlFetchApp.fetch(url, opt);
  var text = response.getContentText();
  Logger.log(text);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top