Call Okta API from Javascript

Hello,

I’m trying to add a user to Okta using the Okta API. I can do that using Postman, using the collection Users (Okta API), in the Create User folder, the Post method Create Activated User With Password.

I need to replicate that from my Javascript code, so I have been trying to do that using the following Ajax call:

           var oktaParameters = {
                "profile": [{ "firstName": "FirstNameValue", "lastName": "LastNameValue", "email": "t1@testEmail.com", "login": "t1@testEmail.com" }],
                "credentials": [{ "password": { "value": "Password5!" }}]
            };
            $.ajax({
                url: 'https://domain.okta.com/api/v1/users?activate=true',
                type: 'POST',
                crossDomain: true,
                dataType: 'jsonp',
                headers: { "Authorization": "Bearer" + "APITOKENVALUE" },
                data: oktaParameters,
                error: function (request, xhr, status, error) {
                    console.log('Error! request.responseText = ', request.responseText);
                    console.log('Error! xhr = ', xhr);
                    console.log('Error! status = ', status);
                    console.log('Error! error = ', error);
                },
                success: function (data) {
                    console.log('Success!');
                }
            });

This Ajax call is not working. I get a parser error in the xhr result, but it doesn’t contain more details that can help me understand what is the problem.

Please share how to do this.

Do you have a space in your Authorization header between Bearer and actual token? Also, don’t you need to JSON.stringify your data?

Also, you shouldn’t be using the API Token if your javascript code is running in a front-end application.
Anyone with access to your source would be able to view your token and make calls to your Okta org.
You can use the “OAuth for Okta” feature to use an access token to call Okta APIs instead - https://developer.okta.com/docs/guides/implement-oauth-for-okta/overview/

it might be backend nodeJS app

@evitoria - If it’s a backend nodejs app, then I’d recommend you use the okta-sdk-nodejs instead of writing your own client functions.