I'm trying to make a $http POST request to Twilio REST API, but I cannot 
get angularJS to authenticate. 

How can I pass the username and password with the $http post? I added a 
$httpProvider.interceptors and passed my username and password but I still 
get user not authorized.

I did do a test on a server side language with all of the same credentials 
and I'm able to connect and send the message ok.

Here is my factory that does the interceptor.

angular.module('interceptors.authInterceptor', [])

.config(function($httpProvider) {
$httpProvider.interceptors.push('authInterceptor');
})

.factory('authInterceptor', function($q) {
return {
request : function(config) {
config.headers = config.headers || {};
config.headers['Authorization'] = '[userid]:[token]';
return config;
},
response : function(response) {
return response || $q.when(response);
},
responseError : function(rejection) {
}
};
})
;

here is my factory that does the $http call

angular.module('tlSMS.messageFactory', [])

.factory('messageFactory', function($http){
var messageObj = {},
accountSid = '[user]',
authToken = '[token]',
twilio_url = 
'https://api.twilio.com/2010-04-01/Accounts/[accountSid]/Messages.json';
messageObj.getNumber = function(groupID){
return $http.get('contact.json')
.success(function(resp){
})
};
messageObj.sendMessage = function(phoneNumber,message){ 
return $http({
method:'post',
url: twilio_url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
     var str = [];
     for(var p in obj)
     str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
     return str.join("&");
},
data: {From:'xxxxx',To:phoneNumber,messageBody:message}
}).success(function(resp){
});
};
return messageObj;
})
;

Anything else I need to do? When I do the post from AngularJS to the URL it 
does not authenticate, if I make the same call using my server side 
language it works fine

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to