目标主机不能为空JSON请求

时间:2015-10-23 09:27:01

标签: java android json oauth yelp

我正在尝试对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 \?另外,我遵循错误中声明的格式,因此我的String应该通过。希望我能得到帮助。

编辑 - 我在我的请求行周围放置了一个URL编码器,并设法通过它发送。但是,我现在收到此消息

目标主机不能为空,或者在参数中设置。 scheme = null,host = null,path = https://api.yelp.com/v2/search?&category_filter=American&cc=AUll=-37.80499706070189,144.9538392573595&limit=10&oauth_consumer_key=yiuyLqUyFVE_-0tOCUkPhw&oauth_nonce=37230bd9-2b3b-4c2d-8d46-39c9c42da743&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1445594839&oauth_token=krEbYb38zOb6Id9shO9bOMjBWuGhBnWz&radius_filter=2000&sort=1&term=American&oauth_signature=36ab3eb5be7cec26c3dacb24cf7906b9426f589a

1 个答案:

答案 0 :(得分:0)

尝试不使用URLEncoder.encode

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(req, YELP_TOKEN_SECRET);
        String arg = "&oauth_signature=";
        final_req = req+arg+signature;
    }catch(Exception e){
        Log.d("YELPEXCEPTION",e.toString());
    }
    new GetPlacesJSONFeed().execute(final_req);