javagithubhttpurlconnectiongithub-release

404 on github release download using java


Why am I getting a 404 here, while downloading a private repo release artifact.

URL url = new URL("https://github.com/anandchakru/private_repo/releases/download/rel_v1/artif-3.0.jar?access_token=myaccesstoken");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        System.out.println(connection.getResponseCode());  //404

and not here, while downloading a public repo release artifact?

URL url = new URL("https://github.com/anandchakru/fr/releases/download/1.0/fr.jar");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        System.out.println(connection.getResponseCode());  //200

Note: I'm able to download https://github.com/anandchakru/private_repo/releases/download/rel_v1/artif-3.0.jar?access_token=myaccesstoken (in an incognito/provate_mode browser).


Solution

  • Ended doing the following:

    1. Add header Accept: application/json
    2. Hit https://api.github.com/repos/anandchakru/private_repo/releases/assets/<asset_id>?access_token=myaccesstoken to fetch the artifact.

    Code:

    URL url = new URL("https://api.github.com/repos/anandchakru/private_repo/releases/assets/<asset_id>?access_token=<token>");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setRequestProperty("Accept", "application/octet-stream");
    ReadableByteChannel uChannel = Channels.newChannel(connection.getInputStream());
    FileOutputStream foStream = new FileOutputStream("C:/Users/username/artif-3.0.jar");
    FileChannel fChannel = foStream.getChannel();
    fChannel.transferFrom(uChannel, 0, Long.MAX_VALUE);
    uChannel.close();
    foStream.close();
    fChannel.close();
    System.out.println(connection.getResponseCode());
    

    Don't forget to close the foStream & uChannel.