Getting 400 Bad Request When POSTing to Get Transaction Token - node.js

I'm trying to integrate our website with Converge API with Hosted Payments Page. Here is the link to their documentation https://developer.elavon.com/#/api/eb6e9106-0172-4305-bc5a-b3ebe832f823.rcosoomi/versions/5180a9f2-741b-439c-bced-5c84a822f39b.rcosoomi/documents?converge-integration-guide/book/integration_methods/../../book/integration_methods/hosted_payments.html
I'm having troubles getting past the first step which is requesting a transaction token from their API endpoint. I'm sending a POST request from my server using axios with the correct parameters and URL, but when I try and POST i get 400 Bad Request. When I make the same request in POSTMAN I get a 200 response with the transaction token. I talked to their developers and they said that everything I was doing was correct and that nothing seemed odd within my code, so even they were stumped as to why I couldn't make a POST request to their endpoint. Obviously there is something within my code that their API is not liking, or else I wouldn't be here trying to find answers for this.
Here is how I'm making the POST request:
app.get('/converge_token_req', (request, response) => {
let params = {
ssl_merchant_id: '*****',
ssl_user_id: '*****',
ssl_pin: '*****',
ssl_transaction_type: 'ccsale',
ssl_amount: '1.00'
}
axios.post('https://api.demo.convergepay.com/hosted-payments/transaction_token', params, {
headers: { 'Content_Type' : 'application/x-www-form-urlencoded' }
}).then((res) => {
response.send(res.data)
}).catch((error) => {
console.log('there was an error getting transaction token')
response.send(error.message)
})
})
Here are the Request Headers:
I'm honestly out of ideas to try. The developers say that everything looks just fine yet I'm unable to make a successful request to their API. If anyone has any thoughts on this that would be great. Thanks!

This code below worked for me:
app.get('/converge_token_req', (request, response) => {
let params = {
ssl_merchant_id: '*****',
ssl_user_id: '*****',
ssl_pin: '*****',
ssl_transaction_type: 'ccsale',
ssl_amount: '1.00'
}
axios({
method: 'post',
url: 'https://api.demo.convergepay.com/hosted-payments/transaction_token',
params: params
}).then((res) => { response.send(res.data)
}).catch((error) => {
console.log('there was an error getting transaction token: ',
error)
})
})

I've since found out the solution to my problem. The issue here is that converge expects a x-www-form-urlencoded string that needs to be Stringified before submitting the request. I found a library that works well for this called qs and I used it like so:
let params = qs.stringify({ // need this if content_type is application/x-www-form-urlencoded
ssl_merchant_id: env.CONVERGE.MERCHANT_ID,
ssl_user_id: env.CONVERGE.USER_ID,
ssl_pin: env.CONVERGE.PIN,
ssl_transaction_type: request.query.type,
ssl_amount: request.query.amount,
ssl_email: request.query.email,
ssl_company: request.query.company,
ssl_avs_address: request.query.address,
ssl_avs_zip: request.query.zip,
ssl_description: request.query.desc,
})
axios.post('https://api.convergepay.com/hosted-payments/transaction_token', params, {
headers: {
'Content_Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
}).then((res) => {
response.send(res.data)
}).catch((error) => {
console.log('there was an error getting transaction token')
response.send(error.message)
})
I think you could also get away with just using JSON.stringify() but this way worked for me.

Related

Unable to call Gitlab API with fetch in javascript

I have below API for fetching all jobs on a project in gitlab:
fetch("https://git.nmlv.nml.com/api/v4/projects/project_id_here/jobs", {
method: 'GET',
headers: {
'Authorization': 'Basic my-token-here'
},
'Accept': 'application/json',
'mode': "no-cors"
})
.then(response => {
console.log("hello")
return response.text();
})
.then(
text => {
var result= JSON.parse(text);
console.log(text)
}).catch(err=>{
console.log(err);
})
The above request works fine in Postman with the same token but here it is saying that the request is unauthorized. The problem is on the Gitlab API docs, they haven't specified how the request in javascript should look like. for your reference, here is the API that I want to call. I know something is incorrect in the way I have framed the API's header. Can anyone help me to find how to frame the request correctly.
Thanks
EDIT
The problem now is that when I run same request on browser inside an html page, response is coming fine. But it is not working inside a node script. The control is not going to then or catch block.

Notion API OAuth Not working `invalid client`

I've tried setting up Notion OAuth and everything works fine on postman but on my application it's not.
I've tried many ways and this is the last implementation I've did so far.
router
.get("/notion/authorize", async (req : Request, res: Response) => {
let redirectURL = `${process.env.NOTION_AUTH_URL}?owner=user&client_id=${process.env.NOTION_CLIENT_ID}&response_type=code&redirect_uri=${encodeURI(process.env.NOTION_REDIRECT_URI)}`
res.redirect(redirectURL)
})
.get("/notion/callback", async (req: Request<any,any, any, {code : string } >, res: Response) => {
try {
const {code} = req.query;
console.log(code)
if(!code) {
throw new Error("Code is required from notion")
}
const response = await axios.post('https://api.notion.com/v1/oauth/token', {
data:JSON.stringify({
"grant_type":"authorization_code",
"code":code,
"redirect_uri": "http://localhost:9000/notion/callback"
}),
headers: {
"Authorization": `Basic ${Buffer.from(`${process.env.NOTION_CLIENT_ID}:${process.env.NOTION_CLIENT_SECRET}`).toString('base64')}`,
"Content-Type": "application/json",
}
})
const {access_token, bot_id, workspace_id, workspace_name} =response.data;
//.. more code
}catch(err) {
if(axios.isAxiosError(err)) {
console.log(err.response.data, err.response.status)
}
return res.status(400).json({message: `Failed to register user ${err}`})
}
})
Stil I get this as error { error: 'invalid_client' } 401
Also yes, checked the keys and redirect URI as well.
Please help me with this :)
It seems like your header is wrong. Check if the field authorization gives the same value as the one you use in postman.
Other thing is your post request. I think you're messing up something. Axios can take one single object when using the request method or 3 parameters (url, data, config) when using the post method (Axios Documentation). In your code, the data and the headers are together and should be apart in different objects.

Not able to res.send() server response, but able to console.log the same response with Express

I'm working on a custom integration solution using Express routes and after making a fetch call to firebase I need the response to my server to be the one coming from firebase to show me any issues (such as authentication errors) coming from there.
I've been trying to display the response by using res.send(), but it throws me a "TypeError: Converting circular structure to JSON" error, but when console.logging that same response it gives me the correct response (which is an authentication error). What's the deal with that?
Here's the code:
router.route("/bankval").get (fetchAirtabeleRecords, (req, res) => {
fetch(`https://xxxxxxxxxxxx.firebaseio.com/integratedData.json?auth=${FIREBASESECRET}`,{
method: 'PUT',
headers:
{
Authorization: 'Bearer ' + FIREBASESECRET,
'Content-Type': 'application/json'
},
body: JSON.stringify({bankValuation: res.bankVal}) // correct values here
})
.then((res) => res.json())
.then(res.send(res)) // This throws me the error
.then((res)=> { console.log('response: ', res); }) // This works and displays expected "error: 'Unauthorized request.' from firebase, but it's only in console, so it's not good enough."
// .then(res.send({bankValuation: res.bankVal})) // this works, but if authentication error occurs it still displays the correct data, when it's obviously not sent to firebase. Only using this for testing purposes.
.catch(err=> { console.log('error: ',err); }) })
I'm pretty new to this, so maybe I'm doing this completely backwards or something, but any input is appreciated.
Thanks.
You override res, so try this:
.then((resp) => resp.json())
.then((resp) => res.send(resp))
.then((resp)=> { console.log('response: ', resp); })

Authorization for Client Credentials Flow (Bad Request)

Authorization for Client Credentials Flow
Hi I have read other Questions or not working properly the current response is 400 (Bad Request) what I had
The following is my code (Authorization has converted Base64)
const testAuth = () => {
return () => {
Axios({
url: 'https://accounts.spotify.com/api/token',
method: 'post',
params: {
grant_type: 'client_credentials'
},
headers: {
'Authorization': 'Basic MWM3NGFkOGQyNDgzNDI0Y2E4NGVmYWRlNzI1MzI5YzE6MDBmMGFmNDE1ZTZhNDgxOThiOWRlYzFmNmE2NTk5NDQ=',
'Content-Type': 'application/x-www-form-urlencoded'
},
}).then((respond) => {
console.log(respond);
}).catch((error) => {
console.log(error);
});
};
But things that are working with returned tokens as well as using spotify wep api normally by using Postman to send out the same content . Is my code uncorrent or is there any problem? (Authorization in Postman is the same as above)
Thank everyone, I just realized is an error of cors. However, there are many ways for trying still can't solve the 400.
For eaxmple: Chrome extension access-control-expose-headers
Preflighted Requests image
400 Respond image

Can't get response from imgur api

I'm trying to get a response from the imgur api, and I'm having trouble getting anything. I keep getting 401 not authorized errors. I've also tried using ".../gallery/authorization?client_id=###########/search="+req.body.search and that doesn't work either. I'm not really understanding the documentation. Can anyone tell me what I am doing wrong? Oh, btw this is written in express/node.js.
router.post('/getimages', function(req,res,next){
console.log('successfully got to /getimages backend :P');
console.log('the value of the search query is ', req.body.search);
var url = 'https://api.imgur.com/3/gallery/search?q='+req.body.search;
axios.get(url)
.then(function (response) {
console.log('YATA from axios to imgur');
console.log(response);
res.json({'YATA':'YATA from getimages'});
})
.catch(function (error) {
console.log('Fail from axios request to imgur');
console.log(error);
res.json({'OHNOES':'NOOOOYATA from getimages'});
});
})
According to the documentation, you have to register your app to use the API. This is even if you'll be using it in an anonymous capacity, which they seem to allow.
Put the clientId in the authorization header :
axios = require("axios");
search = "monkey";
clientId = "YOUR_CLIENT_ID";
axios({
method: 'get',
url: 'https://api.imgur.com/3/gallery/search?q=' + search,
headers: { 'authorization': 'Client-ID ' + clientId }
}).then(function(response) {
console.log(response.data);
}).catch(function(error) {
console.log(error);
});

Resources