3

I need to download a file from my GitHub private repo. So following the instructions on the GitHub site, I created an OAuth token for my credentials.

Then I executed this PS script :

$WebClient = New-Object -TypeName System.Net.WebClient
$WebClient.Headers.Add('Authorization','{OAuth token}')
$uri = "https://github.com/mycompany/myrepo/blob/master/myfile.zip"
$targetPath = "c:\temp"
$WebClient.DownloadFile($uri, $targetPath)

However, $WebClient.DownloadFile() returns a 404. This is strange because I can retrieve the file from $uri via a browser logged-in to GitHub with same credentials used to create OAuth token.

BaltoStar
  • 8,165
  • 17
  • 59
  • 91
  • See this site for a my current solution. http://stackoverflow.com/questions/27951561/use-invoke-webrequest-with-a-username-and-password-for-basic-authentication-on-t – Dean Miller May 23 '16 at 23:49

1 Answers1

2

According to this your two options are HTTPS basic auth and an OAuth token.

So to add basic auth to your webclient try this:

$url = 'https://github.com/mycompany/myrepo/blob/master/myscript.ps1'
$wc = New-Object -TypeName System.Net.WebClient
$wc.Credentials = New-Object -TypeName System.Net.NetworkCredential 'username', 'password'
iex ($wc.DownloadString($url))

To use OAuth you'll need to add a header named Authorization and provide the token string as an argument. Replace the NetworkCredential line from the example above with this.

$wc.Headers.Add('Authorization','token your_token')

Follow the instructions here to create the OAuth token using curl. This part can be done with PowerShell but it's only a one time thing so you can just use the example GitHub provides.

Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • Thanks Andy. I've amended my question to be more clear: I just meant https as a shorthand for an authenticated site to which credentials must be supplied. And yes I need OAuth as I can't commit plain-text credentials to source-control. – BaltoStar May 17 '13 at 10:33
  • I've added some additional instructions. I do not have a private repo to test against however. – Andy Arismendi May 17 '13 at 12:36
  • I configure WebClient Authorization header with OAuth token, but on attempting file-download I get 404. Please see my amended question. – BaltoStar May 21 '13 at 19:03
  • BaltoStar: This seems to be the only post I can find here on StackOverflow on this subject. Don't suppose there have any revelations in the last five months? – Kira Namida Sep 23 '13 at 08:24
  • This solution does not work by the way. Not sure why it is marked as the answer. – JumpingJacks Jan 13 '19 at 15:20
  • Also you can use the `WebClient.DownloadFile(url, path)` signature – ruzenhack Oct 20 '20 at 02:13