I am trying to access the twitter api with angular. Whrn trying the authentication, my code always goes into error. Can someone please help me.
(function(){
angular.module('app')
.controller('MainController', MainController);
function MainController($resource, $http){
var vm = this;
//serviceModule.factory('twitter', function ($resource, $http) {
var consumerKey = encodeURIComponent('xxxxxxxxxxxxxxx');
var consumerSecret = encodeURIComponent('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
var credentials = btoa(consumerKey + ':' + consumerSecret);
console.log(credentials);
// Twitters OAuth service endpoint
var twitterOauthEndpoint = $http.post(
'https://api.twitter.com/oauth2/token',"grant_type=client_credentials",
{headers:{'Authorization':'Basic ' + credentials,
'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8',
}}
);
twitterOauthEndpoint.success(function (response) {
// a successful response will return
// the "bearer" token which is registered
// to the $httpProvider
//serviceModule.$httpProvider.defaults.headers.common['Authorization'] = "Bearer " + response.access_token
console.log("response");
}).error(function (response) {
// error handling to some meaningful extent
console.log("error " + response);
})
}
}());