Get file archive API returns 403

Before raising an issue to the GitLab issue tracker, please read through our guide for finding help to determine the best place to post:

If you are experiencing an issue when using GitLab.com, your first port of call should be the GitLab.com Support Tracker. Your issue may have already been reported there by another user. Please check:

If you feel that your issue can be categorized as a reproducible bug or a feature proposal, please use one of the issue templates provided and include as much information as possible.

Thank you for helping to make GitLab a better product.


I am trying to fetch the repository archive via java code for API: GET /projects/:id/repository/archive as documented in https://docs.gitlab.com/ee/api/repositories.html#get-file-archive 1

I am using PAT for authentication. This API returns a 403. However i am able to call many other APIs to fetch Repositories, branches and they just as expected. Wondering if there is anything specific to this API that i am missing. A call via POSTMAN http client for this API works just fine too.

The URL i am calling is - https://gitlab.com/api/v4/projects/18346998/repository/archive.zip with PAT key as a header parameter.

Here is my sample java code:

Building the URL:
private URL makeGitLabGetArchiveUrl(String endPoint, String version, String repositoryId, String branchName) {
try {
List queryParams = new ArrayList<>();
queryParams.add(new BasicNameValuePair(“sha”, branchName));
URIBuilder uriBuilder = new URIBuilder(endPoint + “/api/” + version + “/projects/” + repositoryId + “/repository/archive.zip”);
return uriBuilder.build().toURL();
} catch (URISyntaxException | MalformedURLException e) {
throw new RenderableException(
ErrorCode.InternalError, “URL is malformed. endPoint:%s, version:%s, repoId:%s”, endPoint, version, repositoryId);
}
}

The Http GET call:
private String doHttpGet(URL url, String personalAccessToken) {
StringBuffer content = new StringBuffer();
Failsafe.with(CONNECTION_RETRY).run(() -> {
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod(“GET”);
// con.setRequestProperty(“Content-Type”, “application/zip”);
con.setRequestProperty(“PRIVATE-TOKEN”, personalAccessToken);
con.setConnectTimeout(5000);
con.setReadTimeout(5000);

        try (BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                content.append(inputLine);
            }
        }
        con.disconnect();
    });

    return content.toString();
}

Thanks in advance for your help.