Sending push notification using ionicframework cloud api - ionic-framework

Hello am trying to send push notification from my nodejs server to ionicframework API and am getting an error here is my code
var token = '66a5c472b52d3210b591f717b5b996312f8xxxxxxxxxxxx';
var title = 'test';
var message = 'message';
var options = {
method: 'POST',
url: 'https://api.ionic.io/push/notifications',
headers: {
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json'
},
json : {
"send_to_all": true,
"profile" : "my-profile",
"notification": {
"title": title,
"message": message,
"android": {
"title": title,
"message": message
},
"ios": {
"title": title,
"message": message
}
}
}
};
request(options, function(err, response, body) {
if (err) throw new Error(err);
console.log(body);
});
am getting this error
{ error:
{ message: 'JWT decode error occurred.',
link: null,
type: 'Unauthorized' },
meta:
{ status: 401,
version: '2.0.0-beta.0',
request_id: '75726406-3060-4329-a59e-3bd7f9ca90c8' } }
What could I be doing wrong

I think there is issue with your authorization header.
In header you're putting token, but please make sure it is API token.
Also make a postman request first and check whether it is working fine.
add content-type and authroization parts in header only..
then check the difference..
Thanks
Basu

Related

Using microsoft graph api to send email from a react application - 401 error

I have a react application that grants access to a user's outlook account. The azure app has delegated permissions for Mail.Send, Mail.ReadWrite and User.Read and when I go through the authorization flow I'm prompted that the application will be able to read and send emails for me. However, after retrieving an access token I get a 401 unauthorized error when sending an email. The logs say the token claims have Mail.Send, Mail.ReadWrite and I'm able to retrieve the user's emails just fine. So I think the token is valid. But sending an email doesn't work and gets a 401 error. Any ideas would be appreciated. Here is my request:
const url = "https://graph.microsoft.com/v1.0/me/sendMail";
const messageToSend = {
subject: "Testing Outlook API",
importance: "Low",
body: {
contentType: "HTML",
content:
"Hello, Testing 1234. This is the body of the message to send. <b>awesome</b>! ",
},
toRecipients: [
{
emailAddress: {
address: "someemail#email.com",
},
},
],
ccRecipients: [
{
emailAddress: {
address: "anotheremail#email.com",
},
},
],
saveToSentItems: true,
};
const headers = {
"Content-Type": "application/json",
Authorization: `Bearer ${microsoftToken}`,
};
axios
.post(url, messageToSend, {
headers: headers,
})
.then((response) => {
console.log("sent mail", response.data);
return response.data;
})
.catch((error) => {
console.log("📛", error);
return false;
});

Always getting error "requestId is required" when doing POST on quickbooks payment API Apps Script

Im creating a script that will process a credit transaction and I always getting this response:
{
"errors": [
{
"code": "PMT-4002",
"type": "invalid_request",
"message": "requestId is required.",
"detail": "requestId",
"infoLink": "https://developer.intuit.com/v2/docs?redirectID=PayErrors"
}
]
}
Im trying to figure out where to put the "request-id" parameter on the request body. Here is my code:
function QBOcreatecharge(){
var token = "TOKEN"
var service = getQuickbooksService();
if (service.hasAccess()) {
var url = 'https://sandbox.api.intuit.com/quickbooks/v4/payments/charges'
var Details =
{
"amount": "80.00",
"currency": "USD",
"capture": "false",
"token": token
}
var params = {
headers: {
Authorization: 'Bearer ' + service.getAccessToken()
},
contentType: 'application/json',
method: 'POST',
payload: JSON.stringify(Details),
muteHttpExceptions:true
}
var response = UrlFetchApp.fetch(url, params);
var value = JSON.parse(response.getContentText())
Logger.log(value)
}
else{
var authorizationUrl = service.getAuthorizationUrl();
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl);
}
}
How do I add the requestId parameter? I tried to insert it on the link, on the header and nothing work. Im using UrlFetch on Google Apps Script. Any help will be appreciated. Thanks!
The Request-Id is a header you need to send. e.g.:
headers: {
Authorization: 'Bearer ' + service.getAccessToken(),
'Request-Id': your unique value here
},
Intuit documents it here:
https://developer.intuit.com/app/developer/qbpayments/docs/develop/explore-the-quickbooks-payments-api/rest-api-features#identifiers

Getting POST Request Message using React.JS

I am writing a program which has a "sign up" functionality. The front-end is created using React.JS. So far, I am able to using this code to send a post request in React.JS:
fetch('http://localhost:2000/users/signup/', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
"email": "testing#gmail.com",
"password": "secret",
"name": "matthew",
"organization": "Apple"
})
}).then(function(response) {
return response.json();
});
This works perfectly - for the user information is now in the database. However I am not sure how to get the response JSON using response.json(). I want to be able to take the response, get the message string, and display it to my user on the front-end. This is the response when I run the same post request on Postman.
{
"message": "New user created"
}
Thanks for the help!
response.json() returns a promise, so you need one more then to get the actual data:
.then(function(response) {
return response.json();
})
.then(function(data) {
console.log(data);
})
see https://developer.mozilla.org/en-US/docs/Web/API/Body/json

Azure Machine Learning REST API: no body

I am simply strying to call the REST API end point of a machine learning experiment created with Azure. I keep getting this error message:
{
"error": {
"code": "BadArgument",
"message": "Invalid argument provided.",
"details": [
{
"code": "RequestBodyInvalid",
"message": "No request body provided or error in deserializing the request body."
}
]
}
}
I have looked it up on their documentation: https://learn.microsoft.com/en-us/azure/machine-learning/machine-learning-web-service-error-codes
All it says is that my bod is empty, I am not sure how can it be empty, here is my code:
router.post('/rating/new', function(req, res) {
var postData = {
"Inputs": {
"input2":
[
{
'Col1': "A11",
'Col2': "6",
'Col3': "A34",
'Col4': "A43",
'Col5': "1169",
'Col6': "A65",
'Col7': "A75",
'Col8': "4",
'Col9': "A93",
'Col10': "A101",
'Col11': "4",
'Col12': "A121",
'Col13': "67",
'Col14': "A143",
'Col15': "A152",
'Col16': "2",
'Col17': "A173",
'Col18': "1",
'Col19': "A192",
'Col20': "A201",
'Col21': "1",
}
],
},
"GlobalParameters": {
}
};
// Configure the request
var options = {
url: config.ML_PREDICTIVE.url,
method: 'POST',
headers: {
'Content-Type':'application/json',
'Authorization':('Bearer ' + config.ML_PREDICTIVE.apiKey)},
form: postData
}
console.log(JSON.stringify(options));
// Start the request
request.post(options, function (error, response, body) {
if(error){
res.status(403).send(error);
}
if(response.statusCode != 200){
res.status(403).send(response.body);
}
if (!error && response.statusCode == 200) {
console.log(body)
}
})
});
The only thing I see that could go wrong is that "form" in the request is not considered body by azure, I have tried with "body" as well no success.
Please help !
According to the code you provided, the variable postData is not valid JSON string. You'd need to use JSON.stringify() method to convert postData value to a JSON string before you send a POST request.

Ionic Push is no longer working 1.x

Everything has been working great then now I get the following error:
ionic.bundle.js:18463 OPTIONS https://push.ionic.io/api/v1/push net::ERR_NAME_NOT_RESOLVED
i cant seem to send a push using the following:
// Encode your key
var auth = btoa(privateKey + ':');
// Build the request object
var req = {
method: 'POST',
url: 'https://push.ionic.io/api/v1/push',
headers: {
'Content-Type': 'application/json',
'X-Ionic-Application-Id': appId,
'Authorization': 'basic ' + auth
},
data: {
"tokens": devices,
"notification": {
"alert": message,
"ios":{
"priority": 10,
"badge": 1,
"payload": {"path": path}
}
}
}
};
// Make the API call
$http(req).success(function(resp){
// Handle success
console.log("Ionic Push: Push success!");
}).error(function(error){
// Handle error
console.log("Ionic Push: Push error...");
});
everything just stopped working - it was working beautifully before.
thanks for your help
I think they have change the URL and format little bit.
New URL is https://api.ionic.io/push/notifications
Payload
{
"tokens":["device_token_1", "device_token_2"],
"profile": "prod",
"notification": {
"title" : "Title of the message",
"message": "Ur message!"
}
}