Google Apps脚本Basecamp POST

时间:2013-03-19 14:58:42

标签: api google-apps-script basecamp

我已经尝试了所有我能想到的组合来完成这项工作。作为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>

1 个答案:

答案 0 :(得分: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作为documented

这是一个工作样本 -

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