【发布时间】:2014-07-06 03:29:14
【问题描述】:
我正在尝试使用 AngularJS 调用 Yelp API,但遇到了麻烦。我不断收到 400 错误请求,我不知道为什么。
Yelp API 文档:
http://www.yelp.com/developers/documentation/v2/authentication http://www.yelp.com/developers/documentation/v2/search_api
包含 Yelp API 生成的密钥的页面:
http://gyazo.com/fa918329eb0cde18a5db242d1d0b0b0e
这是我调用的代码的 sn-p:
function randomString(length, chars) {
var result = '';
for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
return result;
}
app.factory("MyYelpAPI", function($http) {
return {
"retrieveYelp": function(name, callback) {
$http.jsonp("http://api.yelp.com/v2/search?term=food&location=San+Francisco&callback=JSON_CALLBACK",
{
params: {
oauth_consumer_key: /* Consumer Key */,
oauth_token: /* Token */,
oauth_signature_method: "hmac-sha1",
oauth_signature: /* Token Secret */,
oauth_timestamp: new Date().getTime(),
oauth_nonce: randomString(32, '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
}
}
).success(callback);
}
}
});
【问题讨论】: