# When I call API, error occurs

**URL:** <https://community.auth0.com/t/when-i-call-api-error-occurs/111071>\
**Category:** Get Help\
**Tags:** user-profile, user-management\
**Created:** [July 12, 2023, 4:41am UTC](https://community.auth0.com/t/when-i-call-api-error-occurs/111071 "2023-07-12T04:41:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ayethirithan94](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ayethirithan94/32/48754_2.png) [@ayethirithan94](https://community.auth0.com/u/ayethirithan94)\
**Post date:** [July 12, 2023, 4:41am UTC](https://community.auth0.com/t/when-i-call-api-error-occurs/111071/1 "2023-07-12T04:41:47Z")

</div>

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](mailto: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](https://dev-MyCode.jp.auth0.com/api/v2/users)’, jsonUser, this.httpOptions);

---

<div class="post-metadata">

**Author:** ![balake](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/balake/32/48818_2.png) [@balake](https://community.auth0.com/u/balake)\
**Post date:** [July 12, 2023, 3:23pm UTC](https://community.auth0.com/t/when-i-call-api-error-occurs/111071/2 "2023-07-12T15:23:14Z")

</div>

Hi @ayethirithan94 ,

When calling the Management API, you must first retrieve an access token for authorization. [Get Management API Access Tokens for Production](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-access-tokens-for-production)

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

```auto
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:

```auto
{
  "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.

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [July 12, 2023, 7:32pm UTC](https://community.auth0.com/t/when-i-call-api-error-occurs/111071/4 "2023-07-12T19:32:49Z")

</div>

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](https://auth0.com/docs/secure/tokens/access-tokens/get-management-api-tokens-for-single-page-applications).

In order to create a user, you will want to look into [proxying this request](https://community.auth0.com/t/how-can-i-enable-users-to-change-their-email-address-from-a-spa-or-native-app/44064) through a backend using a properly scoped Management API access token.

Cheers!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [July 26, 2023, 7:32pm UTC](https://community.auth0.com/t/when-i-call-api-error-occurs/111071/5 "2023-07-26T19:32:52Z")

</div>

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