如何使用todoist api v7创建todoist任务?

时间:2018-03-20 21:42:53

标签: javascript jquery rest asynchronous todoist

我正试图在todoist中创建一个任务,但似乎无法这样做

根据此todoist documentation,以下内容适用于创建Todoist任务

template<class T>
decltype(x) foo<T>::f1()
{
    return x;
}

我尝试了以下运气很少

$ curl https://todoist.com/api/v7/sync \
    -d token=0123456789abcdef0123456789abcdef01234567 \
    -d sync_token="VRyFHr0Qo3Hr--pzINyT6nax4vW7X2YG5RQlw3lB-6eYOPbSZVJepa62EVhO" \
    -d resource_types='["projects", "items"]' \
    -d commands='[
        { "type": "item_add",
          "temp_id": "fdef5d16-a40a-475e-bd4a-0ccbd6fd8c3f",
          "uuid": "a3aa2f44-23b4-4986-b513-ef7663bbb752",
          "args": { "project_id": "24a193a7-46f7-4314-b984-27b707bd2331", "content": "Task1" } },
        { "type": "item_add",
          "temp_id": "6f5e0b50-af7a-4133-bfc0-e8c041b819d2",
          "uuid": "d16ad84a-e10b-4894-af7d-93ba6adf7a1e",
          "args": { "project_id": 176637191, "content": "Task2" } },
      ]'

我也尝试了以下内容:

commands = [{"type": "item_add", "uuid": "a3aa2f44-23b4-4986-b513-ef7663bbb752", "args": {"project_id": 2159935681,"content":"Test Task"}}]
$.ajax({
      type: "GET",
      url: 'https://en.todoist.com/api/v7/sync/',
      dataType: 'json',
      async: false,
      data: {
        'token': todoist_api_token,
        'sync_token':'*',
        'resource_types':'["projects", "items"]',
        'commands':commands
      }
    })

这会导致以下错误:

commands = [{"type": "item_add", "uuid": "a3aa2f44-23b4-4986-b513-ef7663bbb752", "args": {"project_id": 2159935681,"content":"Test Task"}}]
$.ajax({
      type: "POST",
      url: 'https://en.todoist.com/api/v7/sync/',
      dataType: 'json',
      async: false,
      data: {
        'token': todoist_api_token,
        'sync_token':'*',
        'resource_types':'["projects", "items"]',
        'commands':commands
      }
    })

我也尝试删除项目ID

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://en.todoist.com/api/v7/sync/. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

我也尝试添加temp_id参数:

commands = [{"type": "item_add", "uuid": "a3aa2f44-23b4-4986-b513-ef7663bbb752", "args": {"content":"Test Task"}}]
$.ajax({
      type: "GET",
      url: 'https://en.todoist.com/api/v7/sync/',
      dataType: 'json',
      async: false,
      data: {
        'token': todoist_api_token,
        'sync_token':'*',
        'resource_types':'["items"]',
        'commands':commands
      }
    })

我甚至尝试过todoist api v8版本,遵循v8 api的{todoist指令here

commands = [{"type": "item_add","temp_id": "fdef5d16-a40a-475e-bd4a-0ccbd6fd8c3f", "uuid": "a3aa2f44-23b4-4986-b513-ef7663bbb752", "args": {"project_id": 2159896038,"content":"Test Task"}}]
$.ajax({
      type: "POST",
      url: 'https://en.todoist.com/api/v7/sync/',
      dataType: 'json',
      async: false,
      data: {
        'token': todoist_api_token,
        'sync_token':'*',
        'resource_types':'["projects", "items"]',
        'commands':commands
      }
    })

返回“错误请求”

我确实发现以下适用于v6:

$.ajax({type: "POST",
    url: 'https://beta.todoist.com/API/v8/tasks',
    dataType: 'json',
    async: false,
    data: {'token':todoist_api_token,'content': 'Appointment with Maria'}
    });

1 个答案:

答案 0 :(得分:2)

<强> sync_token

在第一个例子中,我看到设置了同步令牌。它应该是var sync_token = '*',在ajax请求之后,您应该使用sync_token = response.sync_token;保存令牌。我看到您在后面的示例中意识到了这一点。

<强>命令

其余的看起来不错,但我看不到你的命令,我猜问题来自那里。命令对象必须用JSON.stringify(commands)字符串化。

工作示例

我在下面创建了一个工作示例。您必须在示例任务中用您的令牌和项目ID替换todoist_api_token = ""

// Global variables
	
var todoist_api_token = ""; // Put your token here
var sync_token = "*";

// To get a project id: clicke on a project and look at the url.
// In the example "#project%2F2179064046" you have to remove "#project%2F".
// and the project id is 2179064046

// Run example task after document load

window.onload = function() {
	
	console.log("Add example task to todoist");
  
	var example_tasks = [
    {"content": "Task1", "project_id": 2179064046},
    {"content": "Task2", "project_id": 2179064046}
  ];
  
	todoist_add_tasks_ajax(example_tasks);
	
}

// Functions

todoist_add_tasks_ajax = function(tasks) {
		
	var commands = todoist_tasks_to_commands(tasks);
	
	var data = {
		"token" : todoist_api_token,
		'sync_token' : sync_token,
		'resource_types' : '["projects", "items"]',
		'commands' : commands
	};
	
	jQuery.ajax({
		url: "https://todoist.com/api/v7/sync",
		data: data,
		type: "POST",
		dataType: "json",
		success: function(response) {
			console.log(response);
			sync_token = response.sync_token;
		},
		error: function(response) { 
			console.log(response);
		},
	});
	
}

todoist_tasks_to_commands = function(tasks) {
	
	var commands = [];
	
	tasks.forEach(function(args) {
		
		var temp_commands = {
			"type": "item_add",
			"temp_id": create_guid(),
			"uuid": create_guid(),
			"args": args
		};

		commands.push(temp_commands)

	});
	
	commands = JSON.stringify(commands);
	
	return commands;
	
}

function create_guid() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
    s4() + '-' + s4() + s4() + s4();
}

/*

// Install jQuery

javascript: (function(e, s) {
    e.src = s;
    e.onload = function() {
        jQuery.noConflict();
        console.log("jQuery installed");
    };
    document.head.appendChild(e);
})( document.createElement('script'), 'http://code.jquery.com/jquery-latest.min.js')

*/
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js">