Using Python examples - need some to create the JWT-Token (API token)

Hello Forum,

I’m trying out the Python example right now:
Python examples - ChirpStack open-source LoRaWAN® Network Server

But I’m struggling a bit with creating the JWT token:

The API token (retrieved using the web-interface).
api_token = “…”

It’s not really clear to me what I need to do. There is a lot of information here in the forum, but somehow I can’t find the right answer.

I have tried to create an api_token from the ChripStackApplication Server REST API and from JSON Web Tokens. Unfortunately without success in both ways. I always get the message:

_InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAUTHENTICATED
details = “authentication failed: jwt parse error: signature is invalid”
debug_error_string = “{“created”:”@1000000000.200000000",“description”:“Error received from peer ipv4:xxx.xxx.xxx.xxx:8080”,“file”:“src/core/lib/surface/call.cc”,“file_line”:904,“grpc_message”:“authentication failed: jwt parse error: signature is invalid”,“grpc_status”:16}"

Maybe there is a documentation where I can find all the information to create/setup it and I just haven’t found it yet.

Thanks a lot for your help

Here is an example I have used to get total number of end devices added to an application

import requests

auth_token = ''
header = {'Authorization': 'Bearer ' + auth_token}


def getTotalDevicesFromAPI(hostname):
    url = "http://" + str(hostname) + ":8080/api/devices?limit=10&applicationID=1"
    response = requests.get(url, headers=header)
    data = int(response.json()["totalCount"])
    sensor_names = []
    for i in range(data):

        sensor_names.append(response.json()["result"][i]["description"])

    return sensor_names
1 Like

here: https://your_chirpstack_url:8080/#/api-keys

@Jakob Thank you for this example. It can be reproduced very well directly on the server (DeviceService - GET - DeviceService/List). But for running your python code I need to create first the api_token on my side.

@eugenev Thank you for your answer. Does this mean I use the api-key from there or should I create a new one for the api_token = “…”?

When I try to use the existing api-key (Global API keys), it does not work with my authentication in python.

You need token as is, not their ID

@eugenev It isn’t working.

When I create a new global API key to get a token. I get the message:

 _InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
 	status = StatusCode.NOT_FOUND
 	details = "object does not exist"

When I rename ‘Bearer’ to my ‘new name’ I get the message:

 _InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
 	status = StatusCode.UNAUTHENTICATED
 	details = "authentication failed: jwt parse error: illegal base64 data at input byte 9"

The existing API key does not expose the token (only at creation time).

Therefore, I created a new token (clicking on the plus sign), and then I got the actual Token. Copy-pasted that into my source code, and it works.

@tkanerva Thank you for your answer. I tried it again and now it works.

@Jakob and @eugenev Thanks again for your help.