Search code examples
githubgithub-api

GitHub private release asset browser download URL returns HTTP 404 when accessed with Personal Access Token


I have a personal access token that is has Read-only access to Releases in a repository.

enter image description here

I tried using Curl with the access token bearer like below

RELEASE_JSON=$(curl -L \
      -H "Accept: application/vnd.github+json" \
      -H "Authorization: Bearer $ACCESS_TOKEN" \
      -H "X-GitHub-Api-Version: 2022-11-28" \
      $API_BASE_URL/releases/latest)

And then I obtain the download URL for one of the asset files.

DOWNLOAD_URL=$(echo $RELEASE_JSON | 
    jq -r '.assets[] | 
        select(.name | contains("api.yaml")) | .browser_download_url')

It returns a URL correctly. I tried opening the URL in the browser and successfully download the file.

Now I want to download it using CLI to automate some process.

TEMP_FILE=$(mktemp).yaml
curl \
    -o $TEMP_FILE \
    -H "Authorization: Bearer $ACCESS_TOKEN" \
    $DOWNLOAD_URL

When I tried opening the file using cat $TEMP_FILE, the content is Not Found.

What did I do wrong?

UPDATE

I tried using wget


wget \
    --header "Authorization: Bearer $ACCESS_TOKEN" \
    $DOWNLOAD_URL -O $TEMP_FILE

it still returns 404 Not Found.


Solution

  • Turns out I need to use the asset url instead of the browser_download_url. And I need to use header Accept: application/octet-stream.

    This works

    RELEASE_JSON=$(curl -L \
          -H "Accept: application/vnd.github+json" \
          -H "Authorization: Bearer $ACCESS_TOKEN" \
          -H "X-GitHub-Api-Version: 2022-11-28" \
          $API_BASE_URL/releases/latest)
    
    DOWNLOAD_URL=$(echo $RELEASE_JSON | 
        jq -r '.assets[] | 
            select(.name | contains("api.yaml")) | .url')
    
    TEMP_FILE=$(mktemp).yaml
    
    echo "Downloading $DOWNLOAD_URL to $TEMP_FILE"
    curl -L \
          -H "Accept: application/octet-stream" \
          -H "Authorization: Bearer $ACCESS_TOKEN" \
          -H "X-GitHub-Api-Version: 2022-11-28" \
            $DOWNLOAD_URL > $TEMP_FILE