I'm a first time question asker here, my apologies in advance if I make any mistakes in this process. Thank you for your time and assistance in advance.
My end goal is to to use a Node/Express server to access photos from the Webdam.com API. The API docs provide a working CURL command which I converted into a jQuery.ajax() http call that I tested in my browser. I wish to convert the working jQuery.ajax() call into code on a Node/Express server in order to protect my API keys.
In the end I hope to use something along the lines of Axios or Request but I'm open to other options. I've looked into using jQuery on my Node server as is mentioned here, but it appears these code examples are deprecated and because jQuery needs a window.document to work, it seems to me Axios or Request would be a better way to go.
Some background info about the working jQuery.ajax() http call, I first retrieve an Oauth2 token. After I receive a token, I make another jQuery.ajax() call to retrieve the Webdam photo content, API docs are here.
Would someone assist me please in converting this jQuery.ajax() code into Node/Express code so I can retrieve a OAuth2 token.
WORKING CURL COMMAND:
curl -X POST https://apiv2.webdamdb.com/oauth2/token -d 'grant_type=password&client_id=CLIENT_ID&client_secret=SECRET_KEY&username=USERNAME&password=USER_PASSWORD'
WORKING JQUERY.AJAX() CALL:
var params = {
grant_type: 'password',
client_id: CLIENT_ID,
client_secret: SECRET_KEY,
username: USERNAME,
password: USER_PASSWORD
};
$.ajax({
url: 'https://apiv2.webdamdb.com/oauth2/token',
type: 'POST',
dataType: 'json',
data: params,
success: function(response) {
return response.access_token;
},
error: function(error) {
return 'ERROR in webdamAuthenticate';
}
});
const axios = require('axios');
const params = {
grant_type: 'password',
client_id: CLIENT_ID,
client_secret: SECRET_KEY,
username: USERNAME,
password: USER_PASSWORD
};
axios.post('https://apiv2.webdamdb.com/oauth2/token/', params)
.then(response => {
console.log('success in axios webdamAuth', response);
})
.catch(err => {
console.log('ERROR in axios webdamAuth ');
});
This is the error response I get, I thought I was specifying the grant_type but it tells me I am not. I have Googled the error but don't understand what I am missing, please help.
{ error: 'invalid_request',
error_description: 'The grant type was not specified in the request' }
400
{ date: 'Mon, 04 Dec 2017 19:19:34 GMT',
server: 'Apache',
'strict-transport-security': 'max-age=86400; includeSubDomains',
'access-control-allow-origin': '*',
'access-control-allow-headers': 'Authorization, X-XSRF-TOKEN',
'access-control-allow-methods': 'POST, GET, OPTIONS, DELETE, PUT',
'cache-control': 'private, no-cache, no-store, proxy-revalidate, no-transform, max-age=0, must-revalidate',
pragma: 'no-cache',
'content-length': '97',
connection: 'close',
'content-type': 'application/json'}