3

Hey I'm trying to integrate the Yelp API into my cakephp website and get an "Signature was invalid" error.

I tried to find out the reason for my issue with the help of google and stackoverflow, but couldn't solve it

I use the following code:

public function bar($hostel_data=array()){

    $term = "bar";
    $location = "Berlin";
    $test1 = $this->search($term, $location);   

    debug($test1 );  die();         
}




function yelpRequest($path) {

    $consumer_key = "xxxxxxx";
    $consumer_secret = "xxxxxxxx";
    $token = "xxxxxxxxx";
    $token_secret = "xxxxxxxxx";

    require APPLIBS.'OAuth'.DS.'oAuth.php';


    $unsigned_url = "http://" . "api.yelp.com" . $path;

    // Token object built using the OAuth library
    $token = new OAuthToken($token, $token_secret);

    // Consumer object built using the OAuth library
    $consumer = new OAuthConsumer($consumer_key, $consumer_secret);

    // Yelp uses HMAC SHA1 encoding
    $signature_method = new OAuthSignatureMethod_HMAC_SHA1();

    $oauthrequest = OAuthRequest::from_consumer_and_token(
        $consumer, 
        $token, 
        'GET', 
        $unsigned_url
    );

     // Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);

// Get the signed URL
$signed_url = $oauthrequest->to_url();

debug($signed_url);
//utf8_encode($signed_url);

// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);


return $data;
}


function search($term, $location) {

$search_path ="/v2/search/";

$url_params = array();
$url_params['term'] = $term ?: $GLOBALS['DEFAULT_TERM'];
$url_params['location'] = $location?: $GLOBALS['DEFAULT_LOCATION'];
$url_params['limit'] = 20;

$search_path = $search_path . "?" . http_build_query($url_params);

$yelpResponse = $this->yelpRequest($search_path);

debug($url_params); debug($yelpResponse); die();
return $yelpResponse;
}

debug($signed_url):

http://api.yelp.com/v2/search/?limit=20&location=Berlin&oauth_consumer_key=qHsEZDzSkqfT8aOxr79Isw&oauth_nonce=60b30afc5087fcc509eb4245d1e9410c&oauth_signature=XFZFpzdg36G0eG42TZKy5Ny9BbU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1406153682&oauth_token=kqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO&oauth_version=1.0&term=bar

The error response:

'{"error": {"text": "Signature was invalid", "id": "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: GET\u0026http%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch%2F\u0026limit%3D20%26location%3DBerlin%26oauth_consumer_key%3DqHsEZDzSkqfT8aOxr79Isw%26oauth_nonce%3D60b30afc5087fcc509eb4245d1e9410c%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1406153682%26oauth_token%3Dkqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO%26oauth_version%3D1.0%26term%3Dbar"}}'

I hope someone can give some help or a hint how to solve it...

Or is there a good tutorial for cake/php or a plugin for it? Im stuck on this since more than two days :/

Any1
  • 173
  • 1
  • 14

1 Answers1

3

In you code:

$search_path ="/v2/search/";

And your request url($signed_url)

  http://api.yelp.com/v2/search/?limit=20&location=Berlin&oauth_consumer_key=qHsEZDzSkqfT8aOxr79Isw&oauth_nonce=60b30afc5087fcc509eb4245d1e9410c&oauth_signature=XFZFpzdg36G0eG42TZKy5Ny9BbU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1406153682&oauth_token=kqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO&oauth_version=1.0&term=bar

Eror in your url path. THe Search path should be:

$search_path ="/v2/search";

The request url will be changed like this:

http://api.yelp.com/v2/search?limit=20&location=Berlin&oauth_consumer_key=qHsEZDzSkqfT8aOxr79Isw&oauth_nonce=60b30afc5087fcc509eb4245d1e9410c&oauth_signature=XFZFpzdg36G0eG42TZKy5Ny9BbU%3D&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1406153682&oauth_token=kqq8iu-whGeYubw3Q8o_fNyzUgDEpnrO&oauth_version=1.0&term=bar

This is my git repository for the code and google docs fordocumentation.

  • 1
    Thanks for your help dude, unfortunatly it seems this wasnt the only fault.. still get the same after your suggested change in the url – Any1 Jul 24 '14 at 13:12
  • 1
    Yelp connector uses 0auth1.0a [http://oauth.net/core/1.0a] .I think in your code error in creating Signature.When create Signature all the parameters should be alphabetical order. I did that part in java [https://github.com/jarachanthan/Yelp-connector-1.0.0/blob/master/yelp-connector/yelp-connector-1.0.0/src/main/java/org/wso2/carbon/connector/GenerateSignature.java] . – Jarachanthan Ratnakumar Jul 24 '14 at 16:11
  • sorry for the stupid question, but which parameters should be in alphabetical order? – Any1 Jul 24 '14 at 17:09
  • All the parameters(limit,location,oauth_consumer_key,oauth_nonce,oauth_signature,oauth_signature_method,oauth_timestamp,oauth_token,oauth_version&term). After this url parameters. v2/search? – Jarachanthan Ratnakumar Jul 25 '14 at 07:01
  • In my case, I was getting the "Signature was invalid" error because I was generating the signature using a URL different than the one I was finally using. I.e., it didn't have the parameters. Once I used the URL with the parameters, this error went away. – Chris Prince Oct 29 '14 at 19:52