0

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?

Community
  • 1
  • 1
sloth_eyes
  • 127
  • 1
  • 10

1 Answers1

0

I had the same error while I was adding the sort option to my query (which worked before). After seeking the problem it turns out I defined the type of sort as number where Yelp was excepting a string

so

private $sort:number = 0

was wrong and my problem was solved by

private $sort:string = "0"

Regarding your code, I also noticed, that the callback in my code is defined differently respectively

CALLBACK: "angular.callbacks._0"

And maybe just for testing, what happens if you rename your var params to something like yelpParams (a couple of days ago I had a problem in my node backend coz I was using as variable name equals to a predefined request variable. Since then, I try to avoid to use same_name: same_name). Try something like:

 ...{params: yelpParams}...

Furthermore, even I couldn't reproduce it, I read on a forum that Yelp is awaiting a signature with params sorted alphabetically

Hope one of these hints would lead you to solve your problem

David Dal Busco
  • 7,975
  • 15
  • 55
  • 96