Error 400: Access token not found. Google Apps Script

I have a Google Apps Script function that I would like to run a published Particle Cloud function. But I am getting a 400 error. Here is my Apps Script Function:

function sheetOpened() {

    let access_token = "<ACCESS_TOKEN>"
    let device_id = "<DEVICE_ID>"

    let url = "https://api.particle.io/v1/devices/" + device_id + "/sheet_opened";

    let options = {
        method: "POST",
        headers: {
            "Authentication": "Bearer " + access_token
        },
    }

    let response = UrlFetchApp.fetch(url, options);

}

The really weird thing is that this curl request works completely fine:

curl -X POST -H "Authorization: Bearer <ACCESS_TOKEN>" \
https://api.particle.io/v1/devices/<DEVICE_ID>/sheet_opened

And I believe there is not difference between the requests. Does anyone know what is wrong?

I ran UrlFetchApp.getRequest(url, options) and this is the request that was generated:

{
    "url": "https://api.particle.io/v1/devices/<DEVICE_ID>/sheet_opened",
    "payload": "",
    "method": "post",
    "followRedirects": "true",
    "contentType": "application/x-www-form-urlencoded",
    "headers": {
        "X-Forwarded-For": "<REDACTED>",
        "Authentication": "Bearer <ACCESS_TOKEN>"
    },
    "useIntranet": false,
    "validateHttpsCertificates": true
}

Hi, it could be that the POST is missing the body:

not 100% sure though...

I have added a payload to the request:

let options = {
    method: "POST",
    headers: {
        "Authentication": "Bearer " + access_token
    },
    payload: {
        arg: "test"
    }
}

But that didn't seem to do anything. I got the same error
image of error

I would try adding the acces_token in the body too:

This worked! Thanks. My complete code is this:

function sheetOpened() {

    let access_token = "<ACCESS_TOKEN>";
    let device_id =  "<DEVICE-ID>";

    // Url to cloud function (named `sheet_opened`)
    let url = "https://api.particle.io/v1/devices/" + device_id + "/sheet_opened";

    // Request options
    let options = {
        // AJAX Verb, it can work with "GET", but it is probably better to use "POST"
        method: "POST",
        payload: {
            // The `arg` field is not mandatory, but if you want to add it you can
            // arg: "argument"
            access_token: access_token
        }
    }
    
    // Send request, and get response
    let response = UrlFetchApp.fetch(url, options);

    // Log response
    Logger.log(response);
}

Awesome, good work. I always get confused as to where to place the token, if in a header or the body.
Best

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.