I have error when use patchUserMetadata API

Hi, I’m trying to update metadata for user with pathUserMetadata. Bellow is my code:

 management = new auth0.Management({
    domain: environment.domain,
    token: this.cookieService.get('access_token'),
    clientId: environment.clientId,
    clientSecret: environment.cliendSecret,
    scope: 'create:users read:users update:users openid profile email',
  });

this.auth0.management.patchUserMetadata(
        'auth0|xxx' , //of xxx
        userMetadata,
        (err, res) => {
          console.log('err', err);
          console.log('res', res);
        }
      );

but it alway return

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "Bad HTTP authentication header format",
    "errorCode": "Bearer"
}

Pls help me run this api correctly. Thanks!

Hi @tim.dang1,

Welcome to the Auth0 Community!

I understand you have encountered issues using the Management API to update your user’s user_metadata.

Normally when the “Bad header format request” error happens, it is because the Access Token being passed in is not specified as a Bearer.

So in this case, I recommend that you set the Token value to something that looks like the following:

token: 'Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtp....'

Please let me know how this goes for you.

Thanks,
Rueben

Hi, I have double checked about token. The header has been bearer token correctly.

image

When I add Bearer in config, the header become to

authorization: Bearer Bearer eyJhbGciOiJkaX....

I got token from webAuth.login api, I feel it not correct. Do I need to grant permission for it?

Hello, can you help me?

Hi @tim.dang1,

Thank you for your response.

You should use the token that you get from programmatically calling the Management API in a client credentials flow.

For example:

var ManagementClient = require('auth0').ManagementClient;
var management = new ManagementClient({
  domain: '{YOUR_ACCOUNT}.auth0.com',
  clientId: '{YOUR_NON_INTERACTIVE_CLIENT_ID}',
  clientSecret: '{YOUR_NON_INTERACTIVE_CLIENT_SECRET}'
});

Then we can update the user by calling the updateUserMetadata method:

var params = { id: USER_ID };
var metadata = {
  address: '123th Node.js Street'
};

management.updateUserMetadata(params, metadata, function (err, user) {
  if (err) {
    // Handle error.
  }

  // Updated user.
  console.log(user);
});

References:

Please let me know if you have any questions.

Thanks,
Rueben