【发布时间】:2015-10-23 09:27:01
【问题描述】:
我正在尝试查询 Yelp API。但是我一直得到这个结果
{"error": {"text": "Signature was invalid", "id": "INVALID_SIGNATURE", "description": "Invalid signature. Expected signature base string: GET\u0026https%3A%2F%2Fapi.yelp.com%2Fv2%2Fsearch\u0026category_filter%3DAmerican%26cc%3DAUll%253D-37.81107631570312%252C144.96785815805197%26limit%3D10%26oauth_consumer_key%3DyiuyLqUyFVE_-0tOCUkPhw%26oauth_nonce%3Db4f1509d-39ef-4206-8620-1d3dd278c842%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1445592027%26oauth_token%3DkrEbYb38zOb6Id9shO9bOMjBWuGhBnWz%26radius_filter%3D2000%26sort%3D1%26term%3DAmerican"}}
这是我生成请求和 Oauth 签名本身的代码。
private static String hmacSha1(String value, String key)
throws UnsupportedEncodingException, NoSuchAlgorithmException,
InvalidKeyException {
String type = "HmacSHA1";
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
Mac mac = Mac.getInstance(type);
mac.init(secret);
byte[] bytes = mac.doFinal(value.getBytes());
return bytesToHex(bytes);
}
private final static char[] hexArray = "0123456789abcdef".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
我在hmacsha1方法中输入的请求签名如下
String nonsense = UUID.randomUUID().toString();
Long authSeconds = System.currentTimeMillis()/1000;
String req = "https://api.yelp.com/v2/search?"+
"&category_filter=" + chosen+
"&cc=AU"+
"ll=" + x + "," + y+
"&limit=10"+
"&oauth_consumer_key=" + YELP_CONSUMER_KEY+
"&oauth_nonce="+nonsense+
"&oauth_signature_method=HMAC-SHA1"+
"&oauth_timestamp="+authSeconds+
"&oauth_token="+YELP_TOKEN+
"&radius_filter=2000"+
"&sort=1"+
"&term=" + chosen;
String final_req = null;
try{
String signature = hmacSha1(URLEncoder.encode(req), YELP_TOKEN_SECRET);
String arg = "&oauth_signature=";
final_req = URLEncoder.encode(req+arg+signature);
}catch(Exception e){
Log.d("YELPEXCEPTION",e.toString());
}
new GetPlacesJSONFeed().execute(final_req);
我的问题是如何纠正这个问题?我需要在传递的字符串中的某处放置一个 GET\ 吗?另外,我遵循了错误中所述的格式,因此我的字符串应该通过。希望我能得到帮助。
编辑 - 我在我的请求行周围放置了一个 URL 编码器,它设法发送它。但是,我现在收到了这条消息
【问题讨论】:
标签: java android json oauth yelp