How to get user metadata with management API Go SDK

I need to get user metadata in a Go middleware function.

I followed the Go SDK quickstart and have the profile in the request context.

Here are the steps I’m taking. And where it’s going wrong.

Create a new management API instance:

auth0Api, err := management.New(
	auth0Domain,
	management.WithClientCredentials(ctx, auth0ClientId, auth0ClientSecret),
)

Get the userID in the middleware:

id := profile["sub"]

Everything works up to now. This is how I’m trying to get the user metadata with the management API.

user, err := auth0Api.User.Read(
	ctx,
	id, // The id returned from profile["sub"]
	management.IncludeFields("user_metadata", "app_metadata"),
)

This request fails to send. It appears to be sending a get request to https://auth0domain/api/v2/users/userId and a post request to https://auth0domain/oauth/token in the stack trace.

I got most of what I’ve done with the documentation at GitHub - auth0/go-auth0: Go SDK for the Auth0 Management API. and reading the definitions in the library.

But I’m struggling to find any examples on how to make this request.

What am I doing wrong?

What’s the correct way to get the user metadata?

Hi @Outbound5070

I am sorry about the late reply to your post.

The requests that your applications sends seem to be correct ones. Make sure that you have authorized the client with the necessary scopes and that the clientID and secret are the right ones.

I can see that the Go-Auth0 SDK for the Management API does offer some numerous examples inside it for the different Management API calls.

Inside the Management folder, there us a user_test.go file which you can explore for different test functions using the Management API. I have found this function which retrieves the user’s information:

func TestUserManager_Read(t *testing.T) {
	configureHTTPTestRecordings(t)

	expectedUser := givenAUser(t)

	actualUser, err := api.User.Read(context.Background(), expectedUser.GetID())

	assert.NoError(t, err)
	assert.Equal(t, expectedUser.GetID(), actualUser.GetID())
	assert.Equal(t, expectedUser.GetName(), actualUser.GetName())
}

Given this example, you should be able to use something like user.GetAppMetadata() or user.GetUserMetadata().

If you have any other questions or found a solution already, feel free to leave a reply or post on the community!

Kind Regards,
Nik

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.