Hi guys, I try to connect my Go backend to kratos ...
# talk-kratos
d
Hi guys, I try to connect my Go backend to kratos using the code from here https://www.ory.sh/docs/kratos/sdk/go But I always get an 401 Unauthorized. How can I authorize my backend? I cannot find a place where I can set a personal token or something. Thanks!
s
are you using self-hosted or Ory cloud? in the former case, it depends on how you implement auth for the apis, in the latter case have a look https://github.com/ory/sdk/tree/master/clients/kratos/go#documentation-for-authorization
d
Thanks for the quick response! I am using ory cloud.
What is the value of the API key?
s
the PAT you can create on the "connect" page in the console
d
I don't get how to add the PAT to the context. Is there an example somewhere?
f
Pat is personal access token
s
I think all you have to do is
Copy code
c := client.NewAPIClient(&client.Configuration{
		Scheme: "https",
		Host:   "<http://playground.projects.oryapis.com|playground.projects.oryapis.com>",
		DefaultHeader: map[string]string{
			"Authorization": "Bearer ory_pat_xRKLsFEOUFQFVBjd6o3FQDifaLYhabGd",
		},
	})
👍 1
sorry, the docs are really bad around that...
d
Thanks a lot!!
s
did that work?
d
Looks like the authentication works now. But the request to create a new identity fails with error
Index 0 out of range -1
Copy code
apiClient := client.NewAPIClient(&client.Configuration{
		Scheme: "https",
		Debug:  true,
		Host:   "<http://interesting-ishizaka-rms1chovkw.projects.oryapis.com|interesting-ishizaka-rms1chovkw.projects.oryapis.com>",
		DefaultHeader: map[string]string{
			"Authorization": "Bearer ory_pat_XXXXXXXXXXXXXXXXXXXXXXXXX",
		},
	})

	adminCreateIdentityBody := *client.NewAdminCreateIdentityBody(
		"default",
		map[string]interface{}{
			"email": "<mailto:foo@example.com|foo@example.com>",
			"name": map[string]string{
				"first": "foo",
				"last":  "bar",
			},
		},
	) // AdminCreateIdentityBody |  (optional)

	createdIdentity, r, err := apiClient.V0alpha2Api.AdminCreateIdentity(context.Background()).AdminCreateIdentityBody(adminCreateIdentityBody).Execute()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error when calling `V0alpha2Api.AdminCreateIdentity``: %v\n", err)
		fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
	}
	fmt.Fprintf(os.Stdout, "Created identity with ID: %v\n", createdIdentity.Id)
s
hmm ok that one is weird
can you open an issue?
d
sure, I will open an issue