API call - statusCode: 401

I am trying make a API call from Node-Red and it returns 'statusCode: 401'

The same API call from my browser and it is successful.

Why?

[
    {
        "id": "38ae1970b0adf818",
        "type": "inject",
        "z": "81a03fc837d2d50e",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 140,
        "y": 140,
        "wires": [
            [
                "e943e0c4e5782741"
            ]
        ]
    },
    {
        "id": "e943e0c4e5782741",
        "type": "function",
        "z": "81a03fc837d2d50e",
        "name": "function 42",
        "func": "let gateway = 'lemkelabs-5031395368184750'\n\n\nmsg.url = \"https://eu1.cloud.thethings.network/api/v3/gs/gateways/\" + gateway + \"/connection/stats\"\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 310,
        "y": 140,
        "wires": [
            [
                "278737cf884c5a83"
            ]
        ]
    },
    {
        "id": "278737cf884c5a83",
        "type": "http request",
        "z": "81a03fc837d2d50e",
        "name": "",
        "method": "GET",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 510,
        "y": 140,
        "wires": [
            [
                "73eb1bc0464ce853"
            ]
        ]
    },
    {
        "id": "73eb1bc0464ce853",
        "type": "debug",
        "z": "81a03fc837d2d50e",
        "name": "debug 324",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "statusVal": "",
        "statusType": "auto",
        "x": 690,
        "y": 140,
        "wires": []
    }
]

It is telling you that the call is unauthorised. You probably need to pass some authentication header.

I do not add a username or password when I call it from the browser.

The API call

Same error here in firefox browser;

message	"error:pkg/rpcmetadata:unauthenticated (the context is not authenticated)"
details	
0	
@type	"type.googleapis.com/ttn.lorawan.v3.ErrorDetails"
namespace	"pkg/rpcmetadata"
name	"unauthenticated"
message_format	"the context is not authenticated"
correlation_id	"6e33bf34deda43bc8f344f463259b8ca"
code	16

That URL does fail for me in a browser. Possibly you have already logged on the site in some way in the browser.

Your normal browser already has an authentication token in the cache. Possibly a cookie. As Colin says, you probably logged in manually at some point and the cookie has been retained.

You will need to read the docs for the API to see how to do an API-based login.

You need an api key:

https://www.thethingsindustries.com/docs/api/concepts/auth/

grafik

[{"id":"5a85d4e6d42cd8a6","type":"http request","z":"420aeb9f577edec9","name":"ttn","method":"GET","ret":"txt","paytoqs":"ignore","url":"https://eu1.cloud.thethings.network/api/v3/gs/gateways/lemkelabs-5031395368184750/connection/stats","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"Authorization","keyValue":"","valueType":"other","valueValue":"Bearer NNSXS.U4H3ZFFCMSR42BUAZPW2UWGFBV4WCNI5EXDJXDY.SHIF3PP5PBMJNZESN5XLR5TZJTJUIGKVUTM2I22IVBUVCD6VIQIA"}],"x":790,"y":1020,"wires":[["abff19e5d9e4924c"]]},{"id":"14ddae026c87189f","type":"inject","z":"420aeb9f577edec9","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":620,"y":1020,"wires":[["5a85d4e6d42cd8a6"]]},{"id":"abff19e5d9e4924c","type":"debug","z":"420aeb9f577edec9","name":"debug 177","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":990,"y":1020,"wires":[]}]

edit: wrong api version :sweat_smile:

Thank you

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