1

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).

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
  • 3
    Do you have access to the private repo? Have you tried putting those urls into a browser? what happens? – PKey Nov 17 '18 at 19:29
  • Yes, I have access to the private repo. Browser (in incognito mode, which means without my login) is able to download. – Anand Rockzz Nov 17 '18 at 20:19

1 Answers1

1

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.

Anand Rockzz
  • 6,072
  • 5
  • 64
  • 71
  • In case the deprecated method message appears: 1. - Remove ?access_token= from your URL 2.- Just add: connection.setRequestProperty("Authorization", "token YOUR_TOKEN"); – mrddr Dec 06 '20 at 19:03