telstra / messagingapi-sdk-node Goto Github PK
View Code? Open in Web Editor NEWTelstra Messaging SDK - Node Library
Home Page: https://dev.telstra.com
License: Apache License 2.0
Telstra Messaging SDK - Node Library
Home Page: https://dev.telstra.com
License: Apache License 2.0
Hi there,
I'm using the free message API and it can successfully send the message with node.js, here is my code:
`
var TelstraMessaging = require('Telstra_Messaging');
var api = new TelstraMessaging.AuthenticationApi()
var clientId = "MY CID";
var clientSecret = "MY CS";
var grantType = "client_credentials";
var getToken = false;
////////////////////////////////////////////////////
//Input the phone number and messagebody here
var phone = "0411111111";
var messagebody = "Welcome!";
////////////////////////////////////////////////////
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('Generated OAuth Token.');
//console.log(data.access_token);
var OToken = data.access_token;
var defaultClient = TelstraMessaging.ApiClient.instance;
// Configure OAuth2 access token for authorization: auth
var auth = defaultClient.authentications['auth'];
auth.accessToken = data.access_token;
var apiInstance = new TelstraMessaging.ProvisioningApi();
var body = new TelstraMessaging.ProvisionNumberRequest();
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('Created Subscription.');
// console.log(data.destinationAddress);
// console.log(OToken);
var defaultClient = TelstraMessaging.ApiClient.instance;
// Configure OAuth2 access token for authorization: auth
var auth = defaultClient.authentications['auth'];
auth.accessToken = OToken;
var apiInstance = new TelstraMessaging.MessagingApi();
var payload = {
"to": phone,
"body": messagebody
}
// SendSMSRequest | A JSON or XML payload containing the recipient's phone number and text message. This number can be in international format if preceeded by a ‘+’ or in national format ('04xxxxxxxx') where x is a digit.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log("Message sent.");
}
};
apiInstance.sendSMS(payload, callback);
}
};
apiInstance.createSubscription(body, callback);
}
};
api.authToken(clientId, clientSecret, grantType, callback);
`
However, the bundle.js file which generated by browersify cannot successfully in the browser. Here is the error info reported by browser:
`
bundle.js:26 Generated OAuth Token.
2tapi.telstra.com/v2/messages/provisioning/subscriptions:1
Failed to load resource: the server responded with a status of 401 (Unauthorized)
index.html:1 Failed to load https://tapi.telstra.com/v2/messages/provisioning/subscriptions:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 401.
bundle.js:41 Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.
at Request.crossDomainError (bundle.js:3643)
at XMLHttpRequest.xhr.onreadystatechange (bundle.js:3751)
callback @ bundle.js:41
`
This report means I can get OAuth Token succesuffly but I cannot run create subscription successfully.
How can I fix it?
Cheers
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.