I am trying to create a yelp api request in angular and I get an invalid signature error. I have been following a similar problem from here: Yelp API and AngularJS but no luck. Here is my controller:
app.controller('MainController', ['$scope', 'yelpService',
function($scope, yelpService) {
yelpService.retrieveYelp('', function(data) {
$scope.businesses = data.businesses;
});
}]);
And here is my service:
app.factory("yelpService", function($http) {
return {
"retrieveYelp": function(name, callback) {
var method = 'GET';
var url = 'http://api.yelp.com/v2/search';
var params = {
callback: 'JSON_CALLBACK',
location: 'San+Francisco',
oauth_consumer_key: '', //Consumer Key
oauth_token: '', //Token
oauth_signature_method: "HMAC-SHA1",
oauth_timestamp: new Date().getTime(),
oauth_nonce: randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'),
term: 'mexican'
};
var consumerSecret = ''; //Consumer Secret
var tokenSecret = ''; //Token Secret
var signature = oauthSignature.generate(method, url, params, consumerSecret, tokenSecret, { encodeSignature: false});
// var signature = oauth.authorize(request_data, token);
params['oauth_signature'] = signature;
$http.jsonp(url, {params: params})
.then(function(response){
console.log(response);
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response)
})
}
}
});
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
And here is the error:
/**/angular.callbacks._0({"error": {"text": "Signature was invalid", "id": "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: GET\u0026http%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch\u0026callback%3Dangular.callbacks._0%26location%3DSan%252BFrancisco%26oauth_consumer_key%3DiVBpzLwc-Us4-2dlRTzJJg%26oauth_nonce%3DikBZWdLPBMRif0fT0NUi4ygoiUwNXMUq%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1464889608736%26oauth_token%3DY-XgKu4g2wHAK7s_h6NoKwg8rooI797N%26term%3Dmexican"}})
Could anyone help?