0

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 I am using my own api key which is secret so replaced with {{apiKey}}

  • My header looks like this, in the above code.The closing quote is there and I am using my own apiToken in place of {{apiKey}}.Here is a sample of my header from the code.Despite that I am getting this error headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'SSWS 00-5gQI3XgrpxNo6tJEBilxSQ2b' } – Bharat Bhaskar Aug 05 '17 at 02:16
  • You mentioned the error headers, but what is the error code and body you're getting in response? – Milind Gokhale Aug 07 '17 at 20:12
  • Thanks guys the error was due to wrong url . The issue is resolved. – Bharat Bhaskar Aug 08 '17 at 02:55

1 Answers1

0

You seem to be missing a closing quote at

'Authorization': 'SSWS {{apiKey}}

Moreover, I hope you've replaced SSWS {{apiKey}} with your api token in the code. SSWS {{apiKey}} will only work in postman as you might know.

Vijet M
  • 11
  • 1
  • My header looks like this, in the above code.The closing quote is there and I am using my own apiToken in place of {{apiKey}}.Here is a sample of my header from the code.Despite that I am getting this error headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': 'SSWS 00-5gQI3XgrpxNo6tJEBilxSQ2b' } – Bharat Bhaskar Aug 05 '17 at 02:15