Use API by python to get gateway data

Hello ,everyone! I am new in the area of lorawan and chirpstack. I am trying to use API to get gateway data ,but when I use python, I always find an error during the gRPC communication. The following is my code in python.
import requests
import os
import sys
import grpc
import chirpstack_api
from chirpstack_api import api

server = “”

my server host & port

api_token = “”

my api token

channel = grpc.insecure_channel(server)

auth_token = [(“authorization”, “Bearer %s” % api_token)]
def get_data():
client = api.GatewayServiceStub(channel)
req = api.GetGatewayMetricsRequest()
resp = client.GetMetrics(req, metadata=auth_token)
resp.gateway_id = “02819ffffed4f1fd”
resp.start = 1700736381
resp.end = 1701660147
# resp.aggregation = 1
print(resp)

if name == “main”:
get_data()
The following is the error.
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.INTERNAL
details = “Invalid string length”
debug_error_string = “UNKNOWN:Error received from peer {grpc_message:“Invalid string length”, grpc_status:13, created_time:“2023-12-12T03:18:37.688138+00:00”}”

If you have advice in my question, I will be very grateful to you ! Thank you very much!

1 Like

Hi,
Try with:
resp.start.seconds = 1700736381
resp.end.seconds = 1701660147

Not tested with the Python lib, but works with direct API call.

Thank you very much! But what is “direct API call”? How does it run?

You can make direct API calls from Postman for instance.

OK, thank you! I will try it again!

Hey mizi,

In the documentation it shows that the start and end need to be of variable type google.protobuf.Timestamp.

message GetGatewayMetricsRequest {
    // Gateway ID (EUI64).
    string gateway_id = 1;

    // Interval start timestamp.
    google.protobuf.Timestamp start = 2;

    // Interval end timestamp.
    google.protobuf.Timestamp end = 3;

    // Aggregation.
    common.Aggregation aggregation = 4;
}

This seemed to work on my end:

import grpc
from chirpstack_api import api
# Need to import this to create timestamp object
from google.protobuf.timestamp_pb2 import Timestamp

# API interface.
server = "localhost:8080"

# Use this API token: Network API Key
api_token = "token"

# Connect without using TLS.
channel = grpc.insecure_channel(server)

# API client configuration.
gtw_client = api.GatewayServiceStub(channel)

# Define the API key meta-data.
auth_token = [("authorization", "Bearer %s" % api_token)]

try:
    # Create timestamps
    start = Timestamp()
    start.FromSeconds(1700736381)
    end = Timestamp()
    end.FromSeconds(1701660147)

    req = api.GetGatewayMetricsRequest(gateway_id="id", start=start, end=end) 
    resp = gtw_client.GetMetrics(req, metadata=auth_token)
except Exception as e:
    print("Exception occurred: " + str(e))
print(resp)