0

I have searched extensively for a solution but have yet to find success. I just want to be able to download files from my private GitHub repo using PowerShell. I want to use OAuth, not basic auth, so I have generated a token. But from here, none of the examples I've referenced worked for me. Best I could do was get a "Not Found" response.

An example of code I've tried is:

Invoke-WebRequest https://api.github.com/repos/MyAccount/MyRepo/contents/MyFile.txt -Headers @{"Authorization"="token 123456789012345678901234567890"} -OutFile C:\Temp\MyFile.txt

Result:

Invoke-WebRequest : {"message":"Not Found","documentation_url":"https://docs.github.com/rest/reference/repos#get-repository-content"}

I'm fairly confident that I have the authentication right. I believe I just have the path wrong path to my file. Any help would be greatly appreciated.

RaymonS
  • 3
  • 1
  • 5

2 Answers2

0

Potential Duplicate use case relative to this SO discussion ...

PowerShell: retrieve file from GitHub

$url = 'https://github.com/mycompany/myrepo/blob/master/myscript.ps1'
$wc  = New-Object -TypeName System.Net.WebClient
$wc.Headers.Add('Authorization','token your_token')
iex ($wc.DownloadString($url))

.. without Invoke-WebRequest of course.

See also:

Using PowerShell and oAuth

# Modified article code
Invoke-RestMethod https://api.github.com/repos/MyAccount/MyRepo/contents/MyFile.txt -Method Get -Headers @{"Authorization" = "Bearer $accessToken"} 
postanote
  • 15,138
  • 2
  • 14
  • 25
  • I did find and try to use that post already. I get the same "Not Found" response with Invoke-RestMethod and WebClient. Changing up my token at all gives a "Bad Credentials" response, so I know I have the auth part right. I just need to figure out the correct path to my files. – RaymonS Aug 20 '20 at 20:25
0

I had to change the script in Powershell to get it working:

$credentials="<github_access_token>"
$repo = "<user_or_org>/<repo_name>"
$file = "<name_of_asset_file>"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "token $credentials")
$headers.Add("Accept", "application/json")
$download = "https://raw.githubusercontent.com/$repo/main/$file"
Write-Host Dowloading latest release
Invoke-WebRequest -Uri $download -Headers $headers -OutFile $file
cowe
  • 21
  • 5