Docusign legacy header error when making api call - node.js

I am receiving an error when attempting to use legacy headers for Docusign API.
here is my code
request({
headers: {
"X-DocuSign-Authentication": [{
"Username": "zabie#toplevelstaging.com",
"Password": "xxxxxxxx",
"IntegratorKey": "xxxxxxxxxxx-11xxx2f567xxxx0dbxxxx2d"
}]
},
url: "https://demo.docusign.net/restapi/v2/accounts/3465212/envelopes",
json: true, // <--Very important!!!
body: data,
method: "POST",
}, function (error, response, body) {
console.log(response.body);
});
console.log(data[0].templateRoles[0].tabs.textTabs[0].value);
console.log(data[0].templateRoles[0].roleName);
res.redirect('/contracts');
});
Here is the error
{
errorCode: 'INVALID_TOKEN_FORMAT',
message: 'The security token format does not conform to expected schema.'
}

The authheader you are passing is incorrect. Try the following instead. SDK Documentation here
// create JSON formatted auth header
var creds = JSON.stringify({
Username: "zabie#toplevelstaging.com",
Password: "xxxxxxx",
IntegratorKey: "xxxxxxxxxxx-11xxx2f567xxxx0dbxxxx2d"
});
request({
headers: { "X-DocuSign-Authentication": creds },
url: "https://demo.docusign.net/restapi/v2/accounts/3465212/envelopes",
json: true, // <--Very important!!!
body: data,
method: "POST",
}, function (error, response, body) {
console.log(response.body);
});

Related

Auth0 sync userinfo

I'm trying to sync my auth0 userinfo with my local database:
userRouter.get("/sync", validateAccessToken, (req, res) => {
var request = require("request");
var usertoken;
var options = { method: 'POST',
url: 'https://MYDOMAN.eu.auth0.com/oauth/token',
headers: { 'content-type': 'application/json' },
body: '{"client_id":"myclienttoken","client_secret":"myclientsecret","audience":"https://MYDOMAIN.eu.auth0.com/api/v2/","grant_type":"client_credentials"}' };
request(options, function (error, response, body) {
if (error) throw new Error(error);
usertoken = body;
console.log(body);
});
var auth0options = {
method: "GET",
url: "https://MYDOMAIN.eu.auth0.com/api/v2/users",
params: {id: 'email:"testuser"', search_engine: 'v3'},
headers: {
"content-type": "application/json",
authorization: `Bearer` + usertoken.access_token,
},
};
axios.request(auth0options).then(function (response) {
console.log("RES DATA: ", response.data);
})
.catch(function (error) {
console.error(error);
});
console.log("called");
res.status(200).json("message");
});
The following line, results in a error:
authorization: Bearer + usertoken.access_token,
"Cannot read properties of Undefined (reading 'access_token)"
But I don't get the userinfo when calling the auth0 api with that token.
I'm using the audience from the Auth0 Management API ex:
https://MYDOMAIN.eu.auth0.com/api/v2/
And not the audience from my own API, as I have read that's the correct way:
https://mydomain
Any ideas on what I'm doing wrong?

Cannot add data into elastic using axios from nodejs

I am trying to import data into elastic search using axios and nodejs but I am getting 400 bad request error, I have created index in elastic which is sales-enable-data index I am trying to add data using that index but I am getting bad request error
Code
app.get("/import/data", async (req, res) => {
const current = {
project_name: "java",
delivery_manager: "Yawhe",
project_manager: "Ruth",
};
const data = JSON.stringify(current);
await axios({
url: "http://localhost:9200/sales-enable-data-index/_doc",
method: "POST",
headers: {
"Content-Type": "application/json",
},
auth: {
username: "username",
password: "password",
},
body: data,
})
.then((response) => {
return res.status(200).send(response);
})
.catch((err) => {
return res.status(500).send(err);
});
});
Error
"code": "ERR_BAD_REQUEST",
"status": 400
Check the Axios request. Request unable to find the endpoint
You're sending String instead of JSON, this is why ElasticSearch says it does not like the data it's receiving.
Try doing the request without the JSON.stringify like this:
app.get("/import/data", async (req, res) => {
const current = {
project_name: "java",
delivery_manager: "Yawhe",
project_manager: "Ruth",
};
await axios({
url: "http://localhost:9200/sales-enable-data-index/_doc",
method: "POST",
headers: {
"Content-Type": "application/json",
},
auth: {
username: "username",
password: "password",
},
body: current,
})
.then((response) => {
return res.status(200).send(response);
})
.catch((err) => {
return res.status(500).send(err);
});
});

Unable to parse HTTP response body in Node.js

I am making the following request using the node.js request library:
request({
method: 'POST',
uri: TOKEN_ENDPOINT,
'content-type': 'application/x-www-form-urlencoded',
body: JSON.stringify({
code: req.query.code,
client_id: CLIENT_ID,
client_secret: latestSecret.properties.value,
redirect_uri: REDIRECT_URI,
grant_type: 'authorization_code'
})
}, function (error, response, body) {
console.log(body);
console.log(body.access_token);
}
});
When I console.log(body) I get what I expect:
{
"access_token": "...",
"expires_in": 3598,
"scope": "openid",
"token_type": "Bearer"
}
but when I try to console.log body.token_type (or body.scope or other) I get 'undefined.'
I am clearly missing something very simple. Help?
Did you tried JSON.parse(body) before accessing body.token_type?

Perform http request with nodeJS (OAuth2)

I'm have to call an API that use OAuth2 with Client Credentials.
I'm having some trouble to do it...
This is the code I produce (using request package) :
const credentials = {
client: {
id: 'MY_ID',
secret: 'My_PASSWORD'
},
auth: {
tokenHost: 'DOMAIN',
tokenPath: 'PATH',
scope: '',
grantType: "client_credentials"
}
};
var options = {
method: 'POST',
url: credentials.auth.tokenHost + credentials.auth.tokenPath,
headers: { 'content-type': 'application/json' },
body: {
grant_type: credentials.auth.grantType,
client_id: credentials.client.id,
client_secret: credentials.client.secret
},
json: true
};
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body);
});
I have this error :
{ error: 'invalid_request',
error_description: 'Missing form parameter: grant_type' }
Maybe I'm missing something.
It would be very nice of you if you can help me to figure it out :)
PS : It works on Postman so my values are correct.

hapijs yar doesn't work

I am having this in one handler (api/user/login):
request.yar.set('userSession', {userId: doc.userId});
console.log(request.yar.get('userSession'));
in second handler I have this (api/user/isLoggedIn):
console.log(request.yar.get('userSession'));
when I go in browser and do this in console:
$.ajax({
url: 'http://localhost:3004/api/user/login',
method: 'POST',
data: {
email: 'durisvk2#gmail.com',
password: 'jjuurr123'
},
success: function(data) {
console.log(data);
}
})
the server outputs: {userId: 'ASDFGHJKLZX'}
BUT when I do this in Chrome Console:
$.ajax({
url: 'http://localhost:3004/api/user/isLoggedIn',
method: 'GET',
data: {
},
success: function(data) {
console.log(data);
}
})
I get in console undefined
why isn't it storing session from one request to another?

Resources