I set up my express app to listen for Paypal webhooks of my sandbox app. Then I try to verify the integrity of the data via the verify-webhook-signature API endpoint. I use the request module for that. But all I get is a 415 Unsupported Media Type status code and an empty body. This is my code:
app.post('/webhooks', function (req, res) {
if (req.body.event_type == 'PAYMENT.CAPTURE.COMPLETED') {
headers = {
'Accept' : 'application/json',
'Content-Type' : 'application/json',
'Authorization' : 'Bearer <AUTH_TOKEN>'
}
// Get the data for the API call of the webhook request
data = {
'transmission_id': req.headers['paypal-transmission-id'],
'transmission_time': req.headers['paypal-transmission-time'],
'cert_url': req.headers['paypal-cert-url'],
'auth_algo': req.headers['paypal-auth-algo'],
'transmission_sig': req.headers['paypal-transmission-sig'],
'webhook_id': '41G05244UL253035G',
'webhook_event' : req.body
}
request.post({
url: 'https://api.sandbox.paypal.com/v1/notifications/verify-webhook-signature',
headers: headers,
form: data,
}, (error, response, body) => {
if (error) {
console.error(error)
return
}
console.log(response.statusCode);
});
}
res.sendStatus(200);
});
What is wrong with this data?
Edit: Changing 'form: data' to 'body: JSON.stringify(data)' did it.
Why are you sending back a form post? Don't post a form. Send back raw data.
Related
I am learning Node by developing an application that would hit the relevant server and extract its data.
Here, I am stuck as the relevant server demand to pass access token, and I'm not able to extract data successfully.
Here is my code
var options = {
method: 'GET',
url: 'any url',
auth : {
accessToken : "token here"
},
headers: {
'Accept': 'application/json'
}
}
But doing this i'm getting error Error: no auth mechanism defined
Access token seems to be perfect.
Sorry if this has been asked already but I cannot seem to find a straight answer.
Thanks for your efforts and time.
Try this :
var req = {
host: 'YOUR_URL',
method: 'GET',
headers: {
Authorization: your token
'Content-Type': 'application/json'
}
}
request(req, function(error, response, body) {
console.log('error:', error); // Print the error if one occurred
console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
console.log('body:', body);
}
I am trying to handle various http status codes from my node/express and pass the response back to the angular. i get an error that Cannot set Headers after they are sent to the client. How do i Handle this based on response and also log for various http codes in nodejs?
app.post("/employees", function(req,res) {
var ServerOptions = {
method: 'POST',
uri: 'http://localhost:5001/api/empData',
body: req.body,
json: true,
headers: {
'Content-Type': 'application/json'
}
};
request(ServerOptions).then(function (Response) {
res.status(200).json(response);
})
.catch(function (err) {
res.status(401).json({message: "unauthorized"});
console.log("Unauthorized");
res.status(404 || 500).json({message: "error"});
console.log("Error");
res.set("Connection", "close");
});
});
According to angularjs documentation on $http, when server returns status other than 200, "error" callback is invoked.
Try placing your code for handling 401 status inside .error().
I have the following code an i try to connect to a server and get a json response from the server but somthing is wrong becouse i get this error: error null 401. Can you please help me to identify the error on this code or to find what is missing. Thank you!
var request = require('request');
var apiurl = "externalserverurl:6080";
var options = {
"regid" : "username",
"password" : "password",
"uri" : apiurl,
"registrar_domain" : "mydomain.com",
"lang" : "en",
json: true,
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
'User-Agent': 'request'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log('body:', body);
} else {
console.log('error', error, response && response.statusCode);
}
}
request(options, callback);
As #kay already mentioned, errorcode 401 means that your request is unauthorized. Given that there are a lot of different possible authentication methods (basic auth, digest auth, OAuth, you name it), you should refer to your target server and clarify which auth method you should use before trying to actually modify your code.
You could also check the response body, it could contain some hints about what exactly the server expects but doesn't receive.
I'm trying to use the request module to send post request to another service but the callback never fires. Here is what I'm doing right now:
request.post(
`http://localhost:3002/users/login`,
{
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({userDetails})
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
const data = JSON.parse(body);
console.log(data);
} else {
console.log('Request has failed. Please make sure you are logged in');
res.status(401).send(body);
}
}
);
The callback function never fires. On the other hand, if I try to send this exact request with Postman, the server gets the request. What am I doing wrong?
Syntax error maybe? Try with:
request({
method: 'POST',
url: 'http://localhost:3002/users/login',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({userDetails})
},
function (error, response, body) {
// Now it should fire the callback.
console.log('okay');
});
Works like a charm on my side.
Edit: If it still doesn't work, my bet is that the cross-origin resource sharing is not enabled for the localhost:3002 server you're trying to access. Here's the easiest way to enable it.
I have managed to grant access from the user, thus to get the access_token as needed to retrieve the data from the Angellist API.
The next step is to fetch the user data. As we can read from the Angellist API, you do this by authenticated HTTPS GET requests:
Use the Token
Any requests that require authentication must be made
using HTTPS, with the access token provided in the HTTP Authorization
header, request body or query string. AngelList only supports Bearer
tokens, as defined in
https://datatracker.ietf.org/doc/html/draft-ietf-oauth-v2-bearer-08. An example
using the query string:
GET https://api.angel.co/1/me?
access_token=...
To make the request in NodeJS, I use the npm package request as follows:
function testRequest(access_token) {
console.log('test request');
var urltest = "https://api.angel.co/1/me?access_token=" + access_token;
request.get(urltest,
{
'auth': {
'bearer': access_token
}
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log('body', body)
}
console.log('body', body)
console.log('error', error)
}
);
};
However, I keep getting the error:
body {"success":false,"error":{"type":"unauthorized","message":"The authenticated user is not authorized for this request."}}
What am I doing wrong? Does the access_token need to be converted or something, i.e. it is not a bearer token?
Can you try to send the request as follows:
var optionsGetUserInfo = {
method: 'GET',
url: "https://api.angel.co/1/me?access_token=" + access_token",
headers: {
'Authorization': 'Bearer ' + access_token
'Accept' : 'application/json',
'Content-Type' : 'application/json'
}
}
request(optionsGetUserInfo, function (error, response, body) {
if (!error && response.statusCode == 200) {
etc...
I have found out that when trying to sign in with another AngelList account, then it works. The account that I tried before was the same as on which I had registered the client app. Not sure why this makes a difference, but it works.