【问题标题】:'invalid request' error while utilizing wunderlist api in Google Apps Script在 Google Apps 脚本中使用 wunderlist api 时出现“无效请求”错误
【发布时间】:2018-05-20 23:22:31
【问题描述】:

我正在使用 Google Apps Scripts 中的 wunderlist api 从列表 (https://developer.wunderlist.com/documentation/endpoints/task) 中获取任务。以下代码在函数 getTasks 中执行 UrlFetchApp 的行中给出“无效请求”错误。

var accessToken = 'my-access-token';
var clientID = 'my-client-id';
var url = 'https://a.wunderlist.com/api/v1/';

var headers = {
    'X-Access-Token': accessToken,
    'X-Client-Id': clientID,
    'Content-Type': 'application/json'
};

function getTasks(listId){
    var payload = 
    {
        "list_id" : listId,
        "completed" : true
    };
    var options =
    {
        "method" : 'get',
        "headers" : headers,
        "payload" : JSON.stringify(payload),
    };
    var response = UrlFetchApp.fetch(url + 'tasks', options);
    return response;
}

function main(){
    var result = getTasks(my-listid);
}

但是,使用 curl 做同样的事情效果很好;

curl -H "X-Access-Token: my-access-token" -H "X-Client-ID: my-client-id" a.wunderlist.com/api/v1/tasks?list_id=my-list-id

在 Google Apps 脚本中使用另一个使用相同标头的 api 也成功;

function getLists() {
    var options =
        {
            "method" : 'GET',
            "headers" : headers,
        };
    var response = UrlFetchApp.fetch(url + 'lists', options);
    Logger.log(response);
    return response;
}

function main(){
    var result = getLists();
}

我想知道第一个代码有什么问题。提前致谢!

【问题讨论】:

    标签: google-apps-script wunderlist


    【解决方案1】:

    这个请求是 GET 方法。在您的 curl 示例中,list_id=my-list-id 用作查询参数。那么这个修改怎么样呢?

    修改后的脚本:

    var accessToken = 'my-access-token';
    var clientID = 'my-client-id';
    var url = 'https://a.wunderlist.com/api/v1/';
    
    var headers = {
        'X-Access-Token': accessToken,
        'X-Client-Id': clientID,
    //    'Content-Type': 'application/json' // This property may not be necessary.
    };
    
    function getTasks(listId){
        var options =
        {
            "method" : 'get',
            "headers" : headers,
        };
        var q = "?list_id=" + listId + "&completed=true";
        var response = UrlFetchApp.fetch(url + 'tasks' + q, options);
        return response;
    }
    
    function main(){
        var result = getTasks(my-listid);
    }
    

    如果这不起作用,我很抱歉。

    【讨论】:

    • 这工作正常,非常感谢!非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 2021-11-01
    • 2023-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多