Can't get Enqueue Go Example Working

I’ve copied/built the example here: Go examples - ChirpStack open-source LoRaWAN® Network Server documentation

I’ve added a few print lines for debugging, but when running I get this error message: Unauthenticated desc =

Full output here:

ChirpStack Example
Connecting to server...
Creating client...
Enqueueing downlink...
panic: rpc error: code = Unauthenticated desc = 

goroutine 1 [running]:
main.main()
	/home/myuser/repositories/chirp-example/chirpstackexample.go:67 +0x433

Here is the source I’m running exactly

package main

import (
	"context"
	"fmt"

	"github.com/chirpstack/chirpstack/api/go/v4/api"
	"google.golang.org/grpc"
)

// configuration
var (
	// This must point to the API interface
	server = "localhost:8080"

	// The DevEUI for which we want to enqueue the downlink
	devEUI = "0101010101010101"

	// The API token (retrieved using the web-interface)
	apiToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJjaGlycHN0YWNrIiwiaXNzIjoiY2hpcnBzdGFjayIsInN1YiI6ImNlNjNiYmFjLTRlYjAtNGQzYy1hNTA1LTk3NmUz......."
)

type APIToken string

func (a APIToken) GetRequestMetadata(ctx context.Context, url ...string) (map[string]string, error) {
	return map[string]string{
		"authorization": fmt.Sprintf("Bearer %s", a),
	}, nil
}

func (a APIToken) RequireTransportSecurity() bool {
	return false
}

func main() {
	fmt.Println("ChirpStack Example7")

	// define gRPC dial options
	dialOpts := []grpc.DialOption{
		grpc.WithBlock(),
		grpc.WithPerRPCCredentials(APIToken(apiToken)),
		grpc.WithInsecure(), // remove this when using TLS
	}

	// connect to the gRPC server
	fmt.Println("Connecting to server...")
	conn, err := grpc.Dial(server, dialOpts...)
	if err != nil {
		panic(err)
	}

	// define the DeviceService client
	fmt.Println("Creating client...")
	deviceClient := api.NewDeviceServiceClient(conn)

	// make an Enqueue api call
	fmt.Println("Enqueueing downlink...")
	resp, err := deviceClient.Enqueue(context.Background(), &api.EnqueueDeviceQueueItemRequest{
		QueueItem: &api.DeviceQueueItem{
			DevEui:    devEUI,
			FPort:     10,
			Confirmed: false,
			Data:      []byte{0x01, 0x02, 0x03},
		},
	})
	if err != nil {
		panic(err)
	}

	fmt.Printf("The downlink has been enqueued with id: %s\n", resp.Id)
}

It doesn’t seem to like that byte array, but it’s not clear to me what this actually represents and/or what should go there.

I know nothing about Go but by the error “Unautheticated Desc” perhaps your API token is invalid? Did you get it through the webUI? Does the function work with a different “data” value such as an empty array so then you know its not the API token?

I would assume so, I didn’t include the entire thing in the above snippet, but the token in the above is what I got from the UI. I tried generating another with the same output.

I’ve tried Data: []byte{}, with the same error output.

I’m also new to Go language, but I would assume that if the token were bad it would fail at the “dial” step no? conn, err := grpc.Dial(server, dialOpts...)

^That is the step it “hangs” at if there is no server available (Chirpstack isn’t running)

Yes I believe you’re right, especially considering that is the only step that uses the apiToken. If you wanted to give it a shot using the python example it might give you a more verbose explanation to what is wrong or narrow down the issue to either a server side problem or a Go code problem, if not hopefully someone else can help.

1 Like

Mystery solved. I had to add the device EUI used in the example to my devices list! It’s odd to me that it never “joins”, but I suppose that makes sense because it just needs permission to enqueue and never actually maintains a session itself.