keygen-sh/keygen-go

How do I create a license via go code

Closed this issue · 1 comments

This is my simple code for me to get started

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
)

func main() {
	accountId := "MY_ACCOUNT_ID"
	token := "MY_PRODUCT_TOKEN_VALUE"
	url := fmt.Sprintf("https://api.keygen.sh/v1/accounts/%s/licenses", accountId)

	// Define the request body as a map.
	body := map[string]interface{}{
		"data": map[string]interface{}{
			"type": "licenses",
			"relationships": map[string]interface{}{
				"policy": map[string]interface{}{
					"data": map[string]interface{}{
						"type": "policies",
						"id":   "1",
					},
				},
				"user": map[string]interface{}{
					"data": map[string]interface{}{
						"type": "users",
						"id":   "1",
					},
				},
			},
		},
	}

	// Convert the request body to a JSON-encoded byte array.
	jsonBody, err := json.Marshal(body)
	if err != nil {
		panic(err)
	}

	// Create a new HTTP request with the desired method, URL, and body.
	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
	if err != nil {
		panic(err)
	}

	// Set the request headers.
	req.Header.Set("Authorization", "Bearer "+token)
	req.Header.Set("Content-Type", "application/vnd.api+json")
	req.Header.Set("Accept", "application/vnd.api+json")

	// Send the HTTP request and get the response.
	client := &http.Client{}
	res, err := client.Do(req)
	if err != nil {
		panic(err)
	}
	defer res.Body.Close()

	// Read the response body.
	var responseBody map[string]interface{}
	err = json.NewDecoder(res.Body).Decode(&responseBody)
	if err != nil {
		panic(err)
	}

	// Print the response status code and body.
	fmt.Println(res.StatusCode)
	fmt.Println(responseBody)
}

accountId and token, are my account_id and token-value that disappears after creating.
but this is what I get when I try to run this.

> go run main.go
403
map[errors:[map[detail:You do not have permission to complete the request (ensure the token or license is allowed to access all resources) title:Access denied]] meta:map[id:somelong-string]]

What am I doing wrong ? Does this also require passing public-key?

ezekg commented

This isn't related to the Go SDK, so I'm going to close this and follow up via email.