尝试使用路标 oauth 库从 android 客户端调用。我生成了新的 APIv2 密钥,但似乎没有什么不同。我得到的错误看起来像:
{"error":{"text":"Signature was
invalid","id":"INVALID_SIGNATURE","description":"Invalid signature.
Expected signature base string: GET&http%3A%2F%2Fapi.yelp.com
%2Fv2%2Fsearch&category_filter%3Drestaurants%2520food%26limit%3D10%26ll
%3D37.422005%252C-122.084095%26oauth_consumer_key
%3DPbSjw9p08To_XXXXXXXXZg%26oauth_nonce
%3D-81097XXXXXXXX560239%26oauth_signature_method%3DHMAC-
SHA1%26oauth_timestamp%3D129XXXX091%26oauth_token
%3DARfbDXXXXXXXXlBHARTgygLkcMw9h8eW%26oauth_version%3D1.0%26offset
%3D0%26radius_filter%3D300%26sort%3D0"}}
我仔细检查了一下,我所有的令牌和秘密值都与管理 API 屏幕中的内容完全匹配。代码当前如下所示:
if (location != null) {
String lat = Double.toString(location.getLatitude());
String lng = Double.toString(location.getLongitude());
String category = "restaurants+food";
String radius = "300";
String limit = "10";
String offset = "0";
String sort = "0";
try {
String query = String.format("http://api.yelp.com/v2/search?ll=%s,
%s&category_filter=%s&radius_filter=%s&limit=%s&offset=%s&sort=%s",
URLEncoder.encode(lat, ENCODING_SCHEME),
URLEncoder.encode(lng, ENCODING_SCHEME),
category,
URLEncoder.encode(radius, ENCODING_SCHEME),
URLEncoder.encode(limit, ENCODING_SCHEME),
URLEncoder.encode(offset, ENCODING_SCHEME),
URLEncoder.encode(sort, ENCODING_SCHEME));
OAuthConsumer consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY,
CONSUMER_SECRET);
consumer.setTokenWithSecret(TOKEN, TOKEN_SECRET);
consumer.setMessageSigner(new HmacSha1MessageSigner());
consumer.setSendEmptyTokens(true);
consumer.setSigningStrategy(new QueryStringSigningStrategy());
String signedQuery = consumer.sign(query);
Log.i(getClass().getName(), signedQuery);
HttpGet request = new HttpGet(signedQuery);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpClient.execute(request);
HttpEntity entity = response.getEntity();
String jsonResult = EntityUtils.toString(entity);
Log.i(getClass().getName(), jsonResult);