1

我正在尝试从 Google Apps 脚本中查询 Todoist API 中的项目,模仿 curl POST。

我最初试图让 OAuth2 工作,但令牌不是持久的,我转而选择了API 的方法,即使用单个 API 令牌来交换有效令牌。

使用 App Script 的UrlFetchApp 类,我试图在 POST 请求中构造 Todoist 的 API 以检索任务项,并且我的getTodoistToken()函数确实在检索有效的令牌响应,但 POST 命令发出以下 403:

"error_tag":"AUTH_CSRF_ERROR","error_code":0,"http_code":403,"error_extra":{"access_type":"web_session"},"error":"AUTH_CSRF_ERROR"}

谁能推荐一个解决方案?非常感谢,代码如下:

function getTodoistToken() {
  var url = "https://todoist.com/api/access_tokens/migrate_personal_token";
  var data = {
    "client_id": "[my unique client_id]",
    "client_secret": "[my unique client_secret]", 
    "personal_token":"[my API token from Todoist dashboard]", 
    "scope": "data:read"
  };
  var payload = JSON.stringify(data);

  var headers = {
    "Content-Type":"application/json", 
  };

  var options = { 
    "method":"POST",
    "contentType" : "application/json",
    "headers": headers,
    "payload" : payload
  };

  var response = UrlFetchApp.fetch(url, options);
  var json = response.getContentText();
  var data = JSON.parse(json);
  return(data.access_token);  

}

function getTodoistTasks(){
  var apiURL = "https://todoist.com/API/v7/sync";

  var data = {
    "token" : getTodoistToken(),
    "sync_token" : '*',
    "resource_types" : '["items"]'
  };

  var payload = JSON.stringify(data);

  Logger.log(payload);
   var headers = {
     "Content-Type":"application/json", 
   };

  var options = { 
    "method":"POST",
    "contentType" : "application/json",
    "headers": headers,
    "payload" : payload,
    "muteHttpExceptions" : true
  };

  var response = UrlFetchApp.fetch(apiURL, options);

  Logger.log(response.getContentText()); 

}
4

2 回答 2

3

我已经找到答案了。Todoist API 文档有点模棱两可,似乎是围绕 POST 请求编写的,但是要下载(同步)完整的任务列表,一个简单的 URL 编码的 GET 请求(如下所示)就可以了:

function getTodoistTasks(){
  var apiURL = "https://todoist.com/API/v7/sync";
  var queryString = "?token=" + getTodoistTokenRev() + "&sync_token=%27*%27&resource_types=[%22items%22]";

  //Get params
  var fetchParameters = {};
  fetchParameters.method = 'get';
  fetchParameters.contentType = 'x-www-form-urlencoded';
  fetchParameters.muteHttpExceptions = true;

  //make request and return
  var response = UrlFetchApp.fetch(apiURL + queryString, fetchParameters);
  var syncData = JSON.parse(response.getContentText());
  return(syncData);
}
于 2017-06-22T17:23:47.790 回答
0

如果有人正在寻找创建项目的示例(在这种情况下为任务),就像我一样,这里是代码(请注意,您需要指定 date_string 和 due_date 以使其出现在 Web UI 中):

var API_URL = "https://todoist.com/API/v7/sync"
var BASE_QUERY = "?token=" + TOKEN

function addTask() {

//  var taskName = SpreadsheetApp.getUi().prompt('What the task\'s name?')
  var taskName = 'Test 1652'

  var commands = encodeURI(JSON.stringify([{
    "type": "item_add", 
    "temp_id": uuidv4(),
    "uuid": uuidv4(), 
    "args": {
      "content": taskName,
      "date_string": "today",
      "due_date_utc": "2017-12-2T18:00",
    }   
  }]))

  var queryString = BASE_QUERY + '&commands=' + commands

  var options = {
    method: 'post',
    contentType: 'x-www-form-urlencoded',
    muteHttpExceptions: true}

  var response = UrlFetchApp.fetch(API_URL + queryString, options)

  if (response.getResponseCode() !== 200) {
    var content = response.getContentText()
    throw new Error('URL fetch failed: ' + content) 
  }

  var syncData = JSON.parse(response.getContentText())
  return syncData

  // Private Functions
  // -----------------

  function uuidv4() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
      var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
      return v.toString(16);
    });
  }

} // addTask()
于 2017-12-02T17:00:54.250 回答