'How to download a release file from a private git repository

I require to download a file programmatically from the releases section of a private GitHub repository, using powershell.

What I have tried so far:

$url = 'https://github.com/SatisfactoryModdingUE/UnrealEngine/releases/download/4.26.1-css-19/UnrealEngine-CSS-Editor-Win64-1.bin'
iwr -uri $url -outfile .\UnrealEngine-CSS-Editor-Win64-1.bin

This returns error: iwr : The request was aborted: The connection was closed unexpectedly..

This repo is a private repo. I have a username and personal access token that I can use to manually download, but I dont know how to apply these credentials to make the above download script work.



Solution 1:[1]

I found the solution. Thanks to:

powershell script:

function get-latest-repo-release( [string]$gitPersonalAccessToken, [string]$owner, [string]$repo, [string]$dropFolder, $gitApiBase){
  if ('' -eq "$gitApiBase"){
    $gitApiBase = 'api.github.com'}
  $parms = @{uri = "https://$gitApiBase/repos/$owner/$repo/releases"}
  $headers = @{Accept = 'application/vnd.github.v3+json'}
  if ('' -ne "$gitPersonalAccessToken"){
    $headers['Authorization'] = "token $gitPersonalAccessToken"}
  $parms['headers'] = $headers
  $releases = iwr @parms | ConvertFrom-Json
  $latestTag = $releases | sort -Property published_at -Descending | select -first 1 | select -expandProperty tag_name
  $assets = $releases | ?{ $_.tag_name -eq $latestTag}  | select -first 1 | select -expandProperty assets
  $headers['Accept'] = 'application/octet-stream'
  $assets | %{
    $parms['uri'] = $_.url
    $parms['outfile'] = "$dropFolder\$($_.name)"
    iwr @parms}
  $dropFolder}


$gitPersonalAccessToken = '<Redacted>'
$dropFolder = '<Redacted>'
$owner = 'SatisfactoryModdingUE'
$repo = 'UnrealEngine'

$outputDir = get-latest-repo-release -gitPersonalAccessToken $gitPersonalAccessToken -owner $owner -repo $repo -zipDropFolder $dropFolder
write-host "The latest releases can be found in this folder: $outputDir"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1