从BitBucket Rest API v2.0获取文件 [英] Getting a file from BitBucket Rest API v2.0

查看:92
本文介绍了从BitBucket Rest API v2.0获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,该脚本使用bitbucket REST API(1.0)从GIT抓取文件,但是该脚本最近已停止工作.我认为这可能是由于v1 REST API被折旧了,但我不确定.

I have a script which grabs a file from GIT using the bitbucket REST API (1.0) however it has recently stopped working. I'm theorizing this may be due to the v1 REST API being depreciated but I'm not sure.

无论如何,我正在尝试使用新的2.0 REST API检索文件,但是由于请求不断失败,我似乎无法正确理解语法.

Anyway I am trying to retrieve the file using the new 2.0 REST API but I can't seem to get the syntax right as the request continually fails.

因为最容易测试,所以我从卷曲开始.这就是我正在尝试的:

I'm starting out with curl since its easiest to test. This is what I'm trying:

curl -u myusername@mydomain.com "https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/Scripts/Environment Setup/test.txt"

Enter host password for user 'myusername@mydomain.com': redacted

{"type": "error", "error": {"message": "Resource not found", "detail": "There is no API hosted at this URL.\n\nFor information about our API's, please refer to the documentation at: https://developer.atlassian.com/bitbucket/api/2/reference/"}}

这是我使用的参考文档:

Here is the reference documentation I am using: https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/downloads/%7Bfilename%7D

也许我使用了错误的功能?我不确定.

Maybe I am using the wrong function? I'm not sure.

推荐答案

为了后代,您不想使用以下内容从bitbucket下载单个文件:

For posterities sake, you don't want to use the following to download an individual file from bitbucket:

https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/downloads/path/to/your/file.txt

(下载"用于下载整个回购文件,例如.zip文件)

("Downloads" is to download entire repo files like a .zip file)

相反,您想做:

curl --user myuser@mydomain.com:password "https://api.bitbucket.org/2.0/repositories/MyCompany/myrepo/src/master/path/to/file.txt"

如果您尝试使用Invoke-RestRequest(在Powershell中),请注意还有一些额外的步骤.使用旧的1.0 API,您可以执行以下操作:

If you're trying to use Invoke-RestRequest (in powershell) note there are some extra steps. With the old 1.0 API you could do:

$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被禁止.

With the new 2.0 API that no longer works. Powershell's Invoke-RestMethod waits for a 401 response before sending the credentials, and the new 2.0 bitbucket api never provides one, so credentials never get sent causing a 403 forbidden.

要解决此问题,您必须使用以下丑陋的方法来强制Invoke-RestMethod立即在Authorization标头中发送凭据:

To work around that you have to use the following ugly hack to force Invoke-RestMethod to send the credentials immediately in an Authorization header:

$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 

希望这对将来有帮助的人有帮助!

Hopefully that helps someone else out in the future!

感谢@Jim Redmond的帮助.

Thanks @Jim Redmond for the help.

这篇关于从BitBucket Rest API v2.0获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆