REST API - Via Axios

I’m pretty new to this okta world and exploring an option for our auth for our React-based Web and Mobile App. So maybe, I’ve overlooked and don’t know how to use Okta’a REST API via Axios.

The REST API worked just fine in the postman.

Then tried to replicate the same in my React-Native app as below:-

  1. Configured the Axios -
    mport axios from ‘axios’;

const oktaapi = () => axios.create({
baseURL: ‘https://dev-xyz.okta.com’,
headers: {
Authorization: ‘SSWS 00V5dw-p7Y3YsW3VQr8lci_G4uQS9h’
}
});

export default oktaapi;

  1. Invoked the API
    const onPress = async () => {
    try {
    const response = await oktaapi.get(’/api/v1/users?limit=25’);
    console.log(response);
    } catch (error) {
    console.log(error);
    }
    //navigation.navigate(‘Login’);
    };

This didn’t work. I’m getting the following error -
[TypeError: _okta.default.get is not a function. (In ‘_okta.default.get(’/api/v1/users?limit=25’)’, ‘_okta.default.get’ is undefined)]

I’ve no clue what’s going on with this one.

My end goal is to use Okta’s User Type and Users REST APIs to maintain the users.

I’ll appreciate any help.

I need some help on this. I’ve been trying so many different things for a while, but no luck whatsoever.

I tried the below code, which throws a Request failed with status code 403.

const onPress = async () => {
    try {
        //const response = await capi.get('/api/v1/users?limit=25');
        const response = await axios({
            url: 'https://dev-xyz.okta.com/api/v1/users?limit=25',
            method: 'get',
            //baseURL: 'https://dev-71921545.okta.com',
            header: {
                Authorization: 'SSWS <<apikey>>'
            }
        })

        console.log(response.data);
    } catch (error) {
        console.log(error);
    }
    //navigation.navigate('Login');
};

And it works like a charm in Postman. I’ve no clue whats going on with this one.

The Users endpoint is not CORS enabled and you should definitely not be sending an api token along in a front end call, so I hope you are using Axios from a Node server instead…

I could be wrong, but I thought for axios it should be headers instead of header

That you’re getting a 403 is odd and would imply there’s something wrong with how you’re authorizing this call. You might want to check what the network event for this call looks like when its actually made to Okta to see if the Authorization header is being included correctly.

1 Like

Thanks @andrea for the rescue.

How dumb can I be? I couldn’t see the obvious. It should be headers than header.

That’s why people say, “You need a second pair of eyes”.

Thank you.

1 Like

Hi @andrea, One more quick question on this thread -

I’m trying to add a user to a user group (Let’s say - contractors) as shown below -
{
“profile”: {
“login”: “isaac33@xyz.com”,
“email”: “isaac321@{{email-suffix}}”,
“mobilePhone”: “+11234567890”,
“userType”: “Contractor”
},
“credentials”: {
“password” : { “value”: “TestUser123” }
},
“groupIds”: [
“Contractor”
]
}

It works without the user group but throwing an error -
“errorSummary”: “Not found: Resource not found: Contractor (UserGroup)”

I added the user group and assigned the App to it, similar to the “Everyone” group. I think I’m doing something different.

Thanks,

  • Sufian

You need to pass the id for the group you are trying to add the user to. If you pass along its id, instead does it work?

Hi @andrea,

I didn’t find an ID for the group, rather a group name, and I used the name.

-Sufian

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

If you check in the URL when viewing the group in the admin console, you can find its id at the end of the URL, after /admin/group/.

Otherwise, you can use the API to search for a group by its name to get its ID.

1 Like