When I call API, error occurs

I call API for create user from angular, but it isn’t work.
When I check Monitoring log, “Invalid authorization code” error occur.

httpOptions = {
headers: new HttpHeaders({ ‘Content-Type’: ‘application/json’ })
};

let TestUser: any = ;
TestUser.email = “TestUser1@gmail.com”;
TestUser.user_metadata = {};
TestUser.blocked = false;
TestUser.email_verified = false;
TestUser.app_metadata = {};
TestUser.user_id = “testUser_1”;
TestUser.connection = “Username-Password-Authentication”;
TestUser.password = “TestUser1@123”;
TestUser.verify_email = true;

var jsonUser = JSON.stringify(TestUser);
const test = this._objHttp.post(‘https://dev-MyCode.jp.auth0.com/api/v2/users’, jsonUser, this.httpOptions);

Hi @ayethirithan94 ,

When calling the Management API, you must first retrieve an access token for authorization. Get Management API Access Tokens for Production

I don’t know Angular so here is an example in NodeJS:

var options = { method: 'POST',
  url: 'https://dev-mycode.jp.auth0.com/oauth/token',
  headers: { 'content-type': 'application/json' },
  body: '{"client_id":"XXXXXX","client_secret":"XXXXXX","audience":"https://dev-mycode.jp.auth0.com/api/v2/","grant_type":"client_credentials"}' };

This will return the following response:

{
  "access_token": "XXXXXX",
  "token_type": "Bearer"
}

Then use this token in your httpOptions header when creating the user:

headers: { "content-type": 'application/json', "authorization": (token_type + " " + access_token) }

Please let me know if you have any further questions.

1 Like

Thanks for chiming in @balake, and welcome to the community !

@ayethirithan94 In addition to what @balake has said, you shouldn’t be requesting for nor using a Management API Access token on your frontend. The only exception is a minimally scoped token outlined here.

In order to create a user, you will want to look into proxying this request through a backend using a properly scoped Management API access token.

Cheers!

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