Api access from REST to native java


Currently I query devices via REST and create them that way as well in my java application.
Apparently there is a recommendation to better use the gRPC/protobuf interface. This could also increase the query speed.
Unfortunately, I haven’t found any usable Java sample code to understand the concept.
In particular, the query fails because I don’t know where to pass the jwt token.

code snippet:

package ***;
import io.chirpstack.api.as.external.api.DeviceServiceGrpc;
import io.chirpstack.api.as.external.api.GetDevicesSummaryRequest;
import io.chirpstack.api.as.external.api.GetDevicesSummaryResponse;
import io.chirpstack.api.as.external.api.InternalServiceGrpc;
import io.chirpstack.api.as.external.api.LoginRequest;
import io.chirpstack.api.as.external.api.LoginResponse;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class ChirpstackGrpc
	public static void main(String[] args)
		ManagedChannel channel = ManagedChannelBuilder.forAddress("server", 8080)

		InternalServiceGrpc.InternalServiceBlockingStub stub = InternalServiceGrpc.newBlockingStub(channel);
// login to get a jwt token
		LoginRequest loginRequest = LoginRequest.newBuilder()

		LoginResponse loginResponse = stub.login(loginRequest);
		String token = loginResponse.getJwt();
// at this point token is known

// get device list
			req = GetDevicesSummaryRequest.newBuilder()
		GetDevicesSummaryResponse devicesSummary = stub.getDevicesSummary(req);
// > io.grpc.StatusRuntimeException: UNAUTHENTICATED: authentication failed: get token from context error: no authorization-data in metadata




found a solution described in

Write a subclass of io.grpc.CallCredentials and pass it to the InternalServiceBlockingStub instance:

// subclass of io.grpc.CallCredentials
		BearerToken bt = new BearerToken(token);
		GetDevicesSummaryResponse devicesSummary = stub.withCallCredentials(bt).getDevicesSummary(req);