1

我已经尝试了所有我能想到的组合来完成这项工作。我需要将什么作为POST字段发送到 New Basecamp API 以使其接受我的 POST 数据?

我的代码是:

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);
    }

我的错误是:

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

我试过了Utilities.jsonStringify,但没有运气。我知道这是一个菜鸟错误,但我就是想不通。如果有一个地方你知道我也可以从哪里获得这些信息,那将是惊人的。

谢谢!

更新 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);
        }

产量错误:

<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>

4

1 回答 1

1

你确定你正确地尝试过 JSON.stringify 吗?此示例在 Basecamp 中正确地为我创建了一个项目,并在 POST 有效负载中指定了项目名称/描述。

请注意,我写得很快,不包括 UserAgent - 你会想把它放回去。

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);
}

为了更新项目,您必须使用put动词而不是post此处记录

这是一个工作示例 -

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);
}
于 2013-03-19T16:02:56.340 回答