嘿我试图将Yelp API集成到我的cakephp网站中并得到一个" Signature无效"错误。
我试图在google和stackoverflow的帮助下找出问题的原因,但无法解决问题
我使用以下代码:
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;
}
调试($ 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
错误回复:
'{"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"}}'
我希望有人可以提供一些帮助或提示如何解决它......
或者是否有一个很好的教程为cake / php或插件?我被困在这两天以上:/
答案 0 :(得分:3)
在你的代码中:
$search_path ="/v2/search/";
您的请求网址($ 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
您网址路径中的错误。搜索路径应为:
$search_path ="/v2/search";
请求网址将会像这样更改:
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
这是git repository代码和documentation的Google文档。