PHP to Java code conversion with Authorization Headers

Viewed 38

Long time reader, first time question asker on stackoverflow. I writing some code to access the viglinks/sovrn API to pull products programmatically. Using their example, I was able to pull products via PHP with this code

<?php
    $ch = curl_init("https://rest.viglink.com/api/product/search?apiKey=abc&country=de&itemsPerPage=100");
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: secret xyz'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    echo $response;
?>

API and Secret key were obviously changed :) Works great for my purposes. I do get output. But at heart I am a Java guy. So I have attempted to port this code over to Java. Here is what I have so far:

public class myClass {

    public static void main(String[] args) throws Exception {
        String line;
    
        URL url = new URL("https://rest.viglink.com/api/product/search?apiKey=abc&country=de&itemsPerPage=100");//
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("Authorization","secret xyz");
    
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));
        while ((line = in.readLine()) != null)
        {
            System.out.println(line);
        }

    }
}

I run this code, all I receive is a null. I don't receive any crazy codes like a 401. Any ideas or help would be greatly appreciated. Thanks in advance!

0 Answers
Related