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.
Related
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?
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?
i can't figure out what to place in exchange of the JSON.stringify syntax in the body parameter. It is returning a SyntaxError with a code of 800A03EA
const request = require('request');
const username = 'myUserName';
const password = 'myPassword';
const options = {
method: 'POST',
url: 'https://siteToPostTo.com/api/v1/statuses',
auth: {
user: username,
password: password
},
body: JSON.stringify({
status: 'automated message to post'
})
};
request(options, function(err, res, body) {
if (err) {
console.dir(err);
return;
}
console.log('headers', res.headers);
console.log('status code', res.statusCode);
console.log(body);
});
Nothing. Instead, add
json: true to your options and don't attempt any stringification. request() will do the magic for you.
const request = require('request');
const username = 'myUserName';
const password = 'myPassword';
const options = {
method: 'POST',
url: 'https://siteToPostTo.com/api/v1/statuses',
auth: {
user: username,
password: password
},
json: true,
body: {
status: 'automated message to post'
}
};
request(options, function(err, res, body) {
if (err) {
console.dir(err);
return;
}
console.log('headers', res.headers);
console.log('status code', res.statusCode);
console.log(body);
});
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);
});
I have two curl-strings that first do a OAuth2-Token-Request and then load data from the API. Now I want to include this into a node.js plugin so I need to do this from within the plugin.
CURL:
curl -X POST -d 'grant_type=password&client_id=8d3c1664-05ae-47e4-bcdb-477489590aa4&client_secret=4f771f6f-5c10-4104-bbc6-3333f5b11bf9&username=email&password=password' https://api.hello.is/v1/oauth2/token
Test.js:
var request = require('request');
request({
url: 'https://api.hello.is/v1/oauth2/token',
mehtod: "POST",
auth: {
username: 'email',
password: 'password'
},
form: {
'grant_type': 'password',
'client_id': '8d3c1664-05ae-47e4-bcdb-477489590aa4',
'client_secret': '4f771f6f-5c10-4104-bbc6-3333f5b11bf9'
}
}, function(err, res) {
var json = JSON.parse(res.body);
console.log("Access Token:", json.access_token)
});
The problem is that the only thing I get back is: { code: 405, message: 'Method not allowed' } whereas the CURL gives me the the right access_token.
Can anyone help? Thanks!!
Maybe try:
var request = require('request');
request({
url: 'https://api.hello.is/v1/oauth2/token',
mehtod: "POST",
form: {
username: 'email',
password: 'password',
grant_type: 'password',
client_id: '8d3c1664-05ae-47e4-bcdb-477489590aa4',
client_secret: '4f771f6f-5c10-4104-bbc6-3333f5b11bf9'
}
}, function(err, res) {
var json = JSON.parse(res.body);
console.log("Access Token:", json.access_token)
});