<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-M74D8PB" height="0" width="0" style="display:none;visibility:hidden">
Loading
Skip to NavigationSkip to Main Content
0D50Z00008G7UlwSAFOkta Classic EngineIntegrationsAnswered2018-09-05T01:27:31.000Z2017-08-05T02:23:28.000Z2017-08-06T00:47:30.000Z
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.

  
  1.  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.

  • 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!
    Expand Post
This question is closed.
Loading
Invalid token provided error while making an ajax call