Invalid token provided error while making an ajax call Skip to main content
https://support.okta.com/help/answers?id=9062a000000xa9oqac&refurl=http%3a%2f%2fsupport.okta.com%2fhelp%2fanswers
How satisfied are you with the Okta Help Center?
Thank you for your feedback!
How satisfied are you with the Okta Help Center?
1
2
3
4
5
Very Dissatisfied
Very satisfied
Enter content less than 200 characters.
Ask Search:
Bharat BhaskarBharat Bhaskar 

Invalid token provided error while making an ajax call

 I am getting Invalid token provided error . I created new token and with postman, I am able to create user but using ajax call I am not able to create user.Here is the sample code.
 
var express = require('express');
var bodyParser = require('body-parser');
var nodemailer = require('nodemailer');
var request = require('request');
var cookieParser = require('cookie-parser')
 
var port = process.env.port || 9002;
 
var app = express();
 
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
var router = express.Router();
 
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
    next();
});
 
router.post('/createUser', function(req, res) {
 
    var oktaUserCreationData = {
        "profile": {
            "firstName": req.body.profile.firstName,
            "lastName": req.body.profile.lastName,
            "email": req.body.profile.email,
            "login": req.body.profile.webId + '~' + req.body.profile.email
        }
 
    };
    var usderData = JSON.stringify(oktaUserCreationData);
 
    request({
        url: "https://dev-58043.oktapreview.com/api/v1/users?activate=false",
        method: "POST",
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
            'Authorization': 'SSWS {{apiKey}}'
        },
        json: true, // <--Very important!!!
        body: usderData
    }, function(error, response, body) {
        console.log(response);
        ActivatedUser(response.body.id);
        res.json({ "message": "Please check your mail and follow steps, to set your credential." })
    });
 
});
 
app.use('/api', router);
 
app.listen(port, function() {
    console.log('Example app listening on port 9002!')
});

Here in the above code I am using my own apiToken. Can someone tell me what is te issue.
VictorVictor (Okta, Inc.) 
Hello Bharat Bhaskar!

Having a look at your code it seems the Okta API token is missing. You have declared for Authorization: SSWS{{apiKey}}, but is the token declared somewhere else than here?

I used the code provided by Postman for the {{url}}/api/v1/users?activate=false call and the token was declared as well. Here is an example provided from Postman for the same call you are using:

var settings = {
  "async": true,
  "crossDomain": true,
  "url": "yourOktaURL/api/v1/users?activate=false",
  "method": "POST",
  "headers": {
    "accept": "application/json",
    "content-type": "application/json",
    "authorization": "SSWS 00Token....xas.......",
    "cache-control": "no-cache",
    "postman-token": "70......."
  },
  "processData": false,
  "data": "{\n  \"profile\": {\n    \"firstName\": \"Isaac\",\n    \"lastName\": \"Brock\",\n    \"email\": \"isaac@gmail.com\",\n    \"login\": \"isaac@gmail.com\"\n  }\n}"
}

$.ajax(settings).done(function (response) {
  console.log(response);
});

Please let us know if this steps helped you fix the issue!