简体   繁体   中英

How to send authentication key using HttpURLConnection for java android? I am getting error code 500

I am trying to access Skyscanner API through RapidAPI.com. I am taking the provided code snippets and reformatting it for my program. When I run my program I get error code 500 and this link https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0

I tested the endpoints on rapid api and it works fine there.

I am providing the key provided to me by rapidapi.

I have tried running just java code on intellij and java code on android studio. I have tried to use HttpURLConnection and relevant examples on google when searching "How to use HttpURLConnection". I have tried HttpResponse. Same results. I am positive I am sending the key.

    String url = "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0";

    URL obj = new URL(url);
    HttpURLConnection con = (HttpsURLConnection) obj.openConnection();

    // optional default is GET
    con.setRequestMethod("POST");

    //add request header
    con.setRequestProperty("X-RapidAPI-Key", "dfgdgsgMYKEY");
    con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setRequestProperty("inboundDate", "2019-01-10"); //omitted other requests properties
    con.setUseCaches(false);
    con.setDoInput(true);
    con.setDoOutput(true);
    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
     System.out.println(response.toString());

... // my android code attempt

    Future<HttpResponse<JsonNode>> future = Unirest.post("https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/pricing/v1.0")
            .header("X-RapidAPI-Key", "jngoin4ooemgoerg")   //not the actual key
            .header("Content-Type", "application/x-www-form-urlencoded")
            .field("inboundDate", "2019-07-10")
            .field("cabinClass", "business")
            .field("children", 0)
            .field("infants", 0)
            .field("country", "US")
            .field("currency", "USD")
            .field("locale", "en-US")
            .field("originPlace", "SFO-sky")
            .field("destinationPlace", "LHR-sky")
            .field("outboundDate", "2019-06-01")
            .field("adults", 1)
            .asJsonAsync(new Callback<JsonNode>() {

                public void failed(UnirestException e) {
                    System.out.println("The request has failed");
                }

                public void completed(HttpResponse<JsonNode> response) {
                    int code = response.getStatus();
                    Headers headers = response.getHeaders();
                    JsonNode body = response.getBody();
                    InputStream rawBody = response.getRawBody();
                }

                public void cancelled() {
                    System.out.println("The request has been cancelled");
                }

            });

...

EDIT

    String url = "https://skyscanner-skyscanner-flight-search-v1.p.rapidapi.com/apiservices/browsequotes/v1.0/US/USD/en-US/SFO-sky/JFK-sky/2019-09-01";

    URL obj = new URL(url);
    HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

    //add request header
    con.setRequestProperty("X-RapidAPI-Key", "werfwqefqwef");
    //con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
    con.setUseCaches(false);
    con.setDoInput(true);
    con.setDoOutput(true);

    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'GET' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);

    BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();

    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();

    //print result
     System.out.println(response.toString());

I am expecting a JSON string but I am getting error 500.

I believe X-RapidAPI-Host header is missing. Try adding it

x-rapidapi-host: "skyscanner-skyscanner-flight-search-v1.p.rapidapi.com"

PS Skyscanner RapidAPI integration in no longer available. So, I think now it will not work anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM