Custom Widget and RESTful TagoIO API

Custom Widget and RESTful TagoIO API

Hello People!

I have tried to use the RESTful TagoIO API with Javascript inside an Custom Widget, when i get info from an device, it works fine in the first time, but when i try to get data, i receive "Invad Token" or "Authorization denied", and when i back to get info like the first time, i still receive "Authorization denied".

How can i solve this? Or better, which is the correct way? I follow the examples in https://api.docs.tago.io , and i am using the correct device-token to consume the API. My HTML test page:

<!DOCTYPE html>
<html>
    <head>
        <title>Custom Widget</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="text/javascript" src="https://admin.tago.io/dist/custom-widget.min.js"></script>
        <link rel="stylesheet" type="text/css" href="">  
    </head>
    <body>
        <div id="main" >
            <div id="header" ></div>
            <div id="bodyContent" style="overflow: auto; width: 90%; display: inline; white-space: normal;" ></div>
            <div id="foot" ></div>
        </div>
        <script>

            window.TagoIO.onStart((widget) => {
                window.widget = widget;

                let token_device_custom =
                    widget.display.parameters.map( (item) => { if(item.key === 'device_custom') return item.value; } );    
               

                if(token_device_custom){

                    let myHeaders = new Headers();
                    myHeaders.append("device-token", token_device_custom);

                    let requestOptions = {
                        method: 'GET',
                        headers: myHeaders,
                        redirect: 'follow'
                    };

                    fetch("https://api.tago.io/info", requestOptions)
                    .then(response => response.text())
                    .then(result => {
                        let objResult = JSON.parse(result);

                        bodyContent.innerHTML = result; //objResult.result.bucket.id;
                   
                    })
                    .catch(error => bodyContent.innerHTML = 'error: ' + error);

                }

                //bodyContent.innerHTML = JSON.stringify(widget.display);


            });

            window.TagoIO.ready();
        </script>
    </body>
</html>