来自 Android 的 Yelp v2 API 使用 SignPost oauth 库显示签名无效
尝试使用路标 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);
Trying to call from an android client, using the signpost oauth libs. I generated new APIv2 keys, but didn't seem to make a difference. The error I'm getting looks like:
{"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"}}
I double checked, and all my token and secret values exactly match
what's in the manage API screen. Code currently looks like this:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
刚刚发现问题。您应该在设置 TokenSecret 之前添加 MessageSigner,否则它为空并且所有内容都会被严重签名
Just found the issue. You should add the MessageSigner BEFORE setting the TokenSecret, otherwise it's null and everything gets signed badly