The client requires a valid access token to authenticate, you can use the
golang.org/x/oauth2
to easily get one with the Client Credentials OAuth2 flow:
import cc "golang.org/x/oauth2/clientcredentials"
// We need to pass the additional "audience" var to request an access token
additionalValues := url.Values{}
additionalValues.Add("audience", "https://api2.arduino.cc/iot")
if organizationId != "" {
additionalValues.Add("organization_id", organizationId) // Optionally, specify organization
}
// Set up OAuth2 configuration.
config := cc.Config{
ClientID: client,
ClientSecret: secret,
TokenURL: baseURL + "/iot/v1/clients/token",
EndpointParams: additionalValues,
}
// Get the access token in exchange of client_id and client_secret
tok, err := config.Token(context.Background())
if err != nil {
log.Fatalf("Error retrieving access token, %v", err)
}
// Confirm we got the token and print expiration time
log.Printf("Got an access token, will expire on %s", tok.Expiry)
For a working example, see the example folder in this repo.
You can generate Arduino IoT Cloud Client Credentials in the ARDUINO API
section in the IoT Cloud things section:
Client has been re-generated following proper language definitions and using an updated openapi-generator version. Major change is about API naming convetion. Now, all API structures are all defined with uppercase API suffix. For example, 'DevicesV2Api' is now renamed as 'DevicesV2API'.