Skip to content

Issue with browersify #1

@mengjiang1994

Description

@mengjiang1994

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions