Programatically download private repository using Curl

Hi, 

I am going nuts figuring this out , going through tons of google/stackflow/blogs pages but have not found clear answer.

I would like to programatically download repository in ZIP format using Curl and my personal github token.

Assume:

$token -> my personal token

$org -> my organization as a owner of repositories

$repo -> private repository I would like to download

I am trying to use something like this:

curl -H “Authorization: $token” -L https://api.github.com/repos/$org/$repo/zipball > repo.zip

but it produces following json:

{

  “message”: “Not Found”,

  “documentation_url”: “https://developer.github.com/v3/repos/contents/#get-archive-link

}

Please help !!

Hey @dq-shripad,

Thanks for being here! AFAIK the supported and reliable way of downloading an archive file of repository contents programmatically is to correctly authenticate with the API and use the Get archive link API endpoint.

I hope this helps!

1 Like

finally figured out the issue.

The problem was not in curl option/syntax used on command-line.

The problem was in personal access token I had created. I was using an access token created with basic and repo permissions. Aparently, this was not enough.

I then created another access token will full permissions (every permission setting offered ) and with this token I was able to extract repo zip file. 

two things I would like to point out :

  1. The first token created should have worked…not sure why it didn’t. I should not have to create a token will all permissions.

  2. When I used first token, Github API reported that “repositary not fund” even though it existed.!  The message should have been "access token does not have required permissions " or simply "bad credentials ".

Hope Github API team can review this and make changes to their messaging.

2 Likes

Thanks AndreaGriffiths11.

I think the issue was not with authentication but authorization. I have shared my findings.

Hi 

I am also facing a similar kind of issue.

Can you please send me the exact query that you were using.

In my case i need to access the latest release tag name 

and download the zip file.

curl -H “Authorization: token tokenid” https://github.URL.com/orgname/reponame/releases/latest

curl -u username:token https://github.url.com/orgname/reponame/releases/latest

the above two did not work.

You’re missing a "-L " prior to the repo, for instance
Also I don’t know if “releases/latest” work
For me I did “/me/repo/tar.gz/master”

e.g.
curl -H “Authorization: token $GITHUB_MASTER” -L https://codeload.github.com/me/repo/tar.gz/master > repo.tar.gz

This helped me. Thanks!

download_release() {
    TOKEN="xxxxxxxxx"
    REPO=":owner/:repo"
    curl -vLJO https://$TOKEN:@api.github.com/repos/$REPO/zipball/master
}
1 Like

Hey! It seems you can only download a release and it’s subsequent zip file, but this part of the API will allow you to do this: https://developer.github.com/v3/repos/releases/#get-a-single-release-asset

…and for those like me who got here looking for a one-liner command line to clone private repos on other servers, the command is simply:

git clone https:// **yourtoken** @github.com/*user*/*repo*.git

A token can be generated here: https://github.com/settings/tokens

2 Likes