PowerShell:从GitHub检索文件 [英] PowerShell : retrieve file from GitHub
问题描述
我需要从GitHub私有存储库下载文件.因此,按照GitHub网站上的说明,我为自己的凭据创建了OAuth令牌.
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.
然后我执行了这个PS脚本:
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)
但是,$WebClient.DownloadFile()
返回404.这很奇怪,因为我可以通过登录到GitHub的浏览器使用创建OAuth令牌的相同凭据从$uri
检索文件.
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.
推荐答案
According to this your two options are HTTPS basic auth and an OAuth token.
因此要将基本身份验证添加到您的Web客户端,请尝试以下操作:
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))
要使用OAuth,您需要添加一个名为Authorization
的标头并提供令牌字符串作为参数.以此替换上面示例中的NetworkCredential行.
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')
按照说明此处使用 curl 创建OAuth令牌.这部分可以使用PowerShell来完成,但这只是一次性的事情,因此您可以仅使用GitHub提供的示例.
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.
这篇关于PowerShell:从GitHub检索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!