为了后代的缘故,您不想使用以下命令从 bitbucket 下载单个文件:
https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/path/to/your/file.txt https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/path/to/your/file.txt
(“下载”是下载整个存储库文件,如 .zip 文件)
相反,你想做的是:
curl --user [email protected] /cdn-cgi/l/email-protection:password "https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/src/master/path/to/file.txt"
如果您尝试使用 Invoke-RestRequest(在 powershell 中),请注意有一些额外的步骤。使用旧的 1.0 API 你可以这样做:
$cred = Get-Credential
$uri = "https://api.bitbucket.org/1.0/repositories/MyCompany/$($filepath)"
# Get the files from bitbucket (GIT)
Invoke-RestMethod -Credential $cred -Uri $uri -Proxy $proxyUri -OutFile $destination
新的 2.0 API 不再有效。 Powershell 的 Invoke-RestMethod 在发送凭据之前等待 401 响应,而新的 2.0 bitbucket api 从未提供响应,因此凭据永远不会发送,从而导致 403 禁止。
要解决这个问题,您必须使用以下丑陋的黑客来强制 Invoke-RestMethod 立即在授权标头中发送凭据:
$cred = Get-Credential
$uri = "https://api.bitbucket.org/2.0/repositories/MyCompany/$($filepath)"
$username = ($cred.GetNetworkCredential()).username
$password = ($cred.GetNetworkCredential()).password
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
# Get the files from bitbucket (GIT)
Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Uri $uri -Proxy $proxyUri -OutFile $destination
希望这可以帮助其他人在未来!
感谢@Jim Redmond 的帮助。