如何修复TravelPayouts API 403错误-Java

时间:2018-10-26 12:13:53

标签: java

我正在尝试使用travelpayouts API来查找按月分组的最便宜的机票,这需要将令牌作为X-access令牌请求标头传递,但是我的toString返回HTTP / 1.1 403 Forbidden错误

我的代码最初基于以下答案How to convert curl code into java

public String httpGet() {
    String url = "http://api.travelpayouts.com/v1/prices/monthly?currency="+currency+"&origin="+origin+"&destination="+dest+"&token="+token;
    System.out.println(url);
    StringBuilder body = new StringBuilder();
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(url);
    httpGet.setHeader("X-Access-Token", token);

    try {
        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            // System.out.println(statusLine);
            body.append(statusLine + "\n");
            HttpEntity e = response.getEntity();
            String entity = EntityUtils.toString(e);
            body.append(entity);
        } else {
            body.append(statusLine + "\n");
            // System.out.println(statusLine);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpGet.releaseConnection(); // stop connection
    }
    return body.toString(); // return the String
}

任何帮助将不胜感激。

0 个答案:

没有答案