'invalid request' error while utilizing wunderlist api in Google Apps Script

370 Views Asked by At

I'm using wunderlist api from Google Apps Scripts to get tasks from a list (https://developer.wunderlist.com/documentation/endpoints/task). Following code gives 'invalid request' error in the line conducting UrlFetchApp in function getTasks.

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

However, doing the same thing using curl works fine;

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

Utilizing another api using the same header also succeeds in Google Apps Script;

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

I wonder what is wrong in the first code. Thanks in advance!

1

There are 1 best solutions below

1
On BEST ANSWER

This request is GET method. And in your curl sample, list_id=my-list-id is used as a query parameter. So how about this modification?

Modified script :

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

If this didn't work, I'm sorry.