I've been trying to connect AngularJS and Twitter API. It all started with following the instructions on John Lindquist's video on AngularJS twitter search. Which is basically the following code
var app = angular.module("MyApp", ["ngResource"]);
function MyCtrl($scope, $resource) {
var TwitterAPI = $resource("http://search.twitter.com/search.json",
{ callback: "JSON_CALLBACK" },
{ get: { method: "JSONP" }});
$scope.search = function() {
$scope.searchResult = TwitterAPI.get({ q: $scope.searchTerm });
};
}
But the problem is, this generates a 401 Unauthorized. A few searches on google revealed that Twitter had changed its API since this tutorial had been posted. I found another article on using OAuth2 with AngularJS to access Twitter. Which has the following
var consumerKey = encodeURIComponent('<your consumer key>')
var consumerSecret = encodeURIComponent('<your consumer secret>');
var credentials = Base64.encode(consumerKey + ':' + consumerSecret);
// 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'}}
);
However this also ended up with the following error on the console
> OPTIONS https://api.twitter.com/oauth2/token 405 (Method Not Allowed)
> XMLHttpRequest cannot load https://api.twitter.com/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:3000' is therefore not allowed access.
I've gone this far, but don't know what to do next to get search results using $resource from twitter. Don't know if this helps, but I'm also using Firebase.