【问题标题】:Target host must not be null JSON Request目标主机不能为空 JSON 请求
【发布时间】: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 编码器,它设法发送它。但是,我现在收到了这条消息

目标主机不能为空,也不能在参数中设置。方案=null,主机=null,路径=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

【问题讨论】:

    标签: java android json oauth yelp


    【解决方案1】:

    尝试不使用 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);
    

    【讨论】:

    • 如果没有 URL 编码,我会得到: {"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.81221290867261%252C144.955616556108%26limit%3D10%26oauth_consumer_key%3DyiuyLqUyFVE_-0tOCUkPhw%26oauth_nonce %3D6ba3c3c5-906d-4fff-ab6a-3effe7ac4d58%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1445605461%26oauth_token%3DkrEbYb38zOb6Id9shO9bOMjBWuGhBnWz%26radius_filter%3D2000%26sort%3D1%26term%3DAmerican"}}
    • 请问这是传递请求的正确方法还是有其他方法?我的请求字符串的格式也是错误消息希望的格式。
    • 我不知道正确的方法,因为我没有使用 YELP API。但我认为 UrlEncode 损坏了您的 url,YELP API 无法识别您的请求和密钥。
    • 有没有办法在发送 url 之前检查结果是否正确?或者有没有使用其他 API 的例子?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-21
    • 2018-10-21
    • 2020-09-19
    • 2013-08-07
    • 1970-01-01
    • 2014-06-27
    相关资源
    最近更新 更多