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

Javascript 如何使用todoist api v7创建todoist任务?,javascript,jquery,rest,asynchronous,todoist,Javascript,Jquery,Rest,Asynchronous,Todoist,我试图在todoist中创建一个任务,但似乎无法这样做 根据这一点,下面的代码应该用于创建todoist任务 $ curl https://todoist.com/api/v7/sync \ -d token=0123456789abcdef0123456789abcdef01234567 \ -d sync_token="VRyFHr0Qo3Hr--pzINyT6nax4vW7X2YG5RQlw3lB-6eYOPbSZVJepa62EVhO" \ -d resource

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

根据这一点,下面的代码应该用于创建todoist任务

$ 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
      }
    })
这将导致以下错误:

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).
我还尝试删除项目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
      }
    })
我还尝试添加了temp_id参数:

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
      }
    })
我甚至按照v8 api的todoist说明尝试了todoist api v8版本

这将返回“错误请求”

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

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

同步令牌

在第一个示例中,我看到设置了sync令牌。它应该是
var-sync\u-token='*'
并且在ajax请求之后,您应该使用
sync\u-token=response.sync\u-token保存令牌我看到您在后面的示例中意识到了这一点

命令

其余的看起来不错,但我看不到你的命令,我想问题就出在那里。commands对象必须使用
JSON.stringify(commands)
进行字符串化

工作示例

我在下面创建了一个工作示例。您必须在示例任务中将
todoist\u api\u token=”“
替换为您的token和项目id

//全局变量
var todoist_api_token=“”;//把你的代币放在这里
var sync_令牌=“*”;
//要获取项目id:单击项目并查看url。
//在示例“项目%2F2179064046”中,必须删除“项目%2F”。
//项目编号为2179064046
//加载文档后运行示例任务
window.onload=函数(){
log(“将示例任务添加到todoist”);
变量示例_任务=[
{“内容”:“任务1”,“项目id”:2179064046},
{“内容”:“任务2”,“项目id”:2179064046}
];
todoist\u add\u tasks\u ajax(示例任务);
}
//功能
todoist\u add\u tasks\u ajax=函数(任务){
var commands=todoist_tasks_to_commands(tasks);
风险值数据={
“令牌”:todoist_api_令牌,
“同步令牌”:同步令牌,
“资源类型”:“[“项目”、“项目”],
“命令”:命令
};
jQuery.ajax({
url:“https://todoist.com/api/v7/sync",
数据:数据,
类型:“POST”,
数据类型:“json”,
成功:功能(响应){
控制台日志(响应);
sync_令牌=response.sync_令牌;
},
错误:函数(响应){
控制台日志(响应);
},
});
}
todoist_任务\u to_命令=函数(任务){
var命令=[];
tasks.forEach(函数(args){
var temp_命令={
“类型”:“添加项目”,
“临时id”:创建临时guid(),
“uuid”:创建_guid(),
“args”:args
};
命令。推送(临时命令)
});
commands=JSON.stringify(commands);
返回命令;
}
函数create_guid(){
函数s4(){
返回Math.floor((1+Math.random())*0x10000)
.toString(16)
.子串(1);
}
返回s4()+s4()+'-'+s4()+'-'+s4()+'-''+
s4()+'-'+s4()+s4()+s4()+s4();
}
/*
//安装jQuery
javascript:(函数(e,s){
e、 src=s;
e、 onload=函数(){
jQuery.noConflict();
log(“jQuery已安装”);
};
文件.标题.附件(e);
})(document.createElement('script'),'http://code.jquery.com/jquery-latest.min.js')
*/
$.ajax({type: "POST",
    url: 'https://todoist.com/API/v6/add_item',
    dataType: 'json',
    async: false,
    data: {'token':todoist_api_token,'content': 'Appointment with Maria'}
    });