使用Powershell上传到Bitbucket下载部分

时间:2015-03-26 12:29:18

标签: powershell curl file-upload bitbucket

所以我们最近切换到Bitbucket - 喜欢不同的定价模式/分支和回购权限。但是,我真的很想念GitHub中的Releases功能。

然而,BB确实有"下载"我们可以将最近的二进制文件上传到的部分。但是......它没有通过他们的API公开。

我找到了一个使用cURL的漂亮脚本,但我想在PowerShell中这样做,所以我不需要任何特殊程序(我们使用AppVeyor作为我们的CI服务器,没有cURL)。

我主要工作 - 过程是:

  1. 加载登录页面以获取CSRF令牌 - 正常工作
  2. 登录,重定向到下载页面 - 正常工作
  3. 上传文件 - 失败
  4. 我的代码是here

    我已将问题隔离到表单数据中。当我自己上传时,这就是fiddler的样子: Fiddler snip manual upload

    但是当我使用Powershell时,我无法让它填充#34; WebForms"请求的标签。我尝试使用-InFile并指定要上传的文件,但Bitbucket也需要显示其他参数。

    如何指定一系列"表格字段"用Powershell包含在请求中吗?

    修改 在这里添加代码,即使它使这个混乱。

    $baseUri = "https://bitbucket.org"
    $teamName = "xxxxxxxxx"
    $projectName = "xxxxxxxxxxxxxxx"
    $page = "/$teamName/$projectName/downloads"
    $uri = "https://bitbucket.org/account/signin/"
    $username = "xxxxxxxxxxxxxxxx"
    $password = "xxxxxxxxxxxxxxxx"
    $headers = @{
      "referer" = "$uri"
    }
    $json = @"
    {
        "username":"$username",
        "password":"$password",
        "submit":"",
        "next":"$page",
        "csrfmiddlewaretoken":""
     }
    "@
    
    $formData = @"
    {
        "token":"",
        "csrfmiddlewaretoken":""
     }
    "@
    
    $body = $json | ConvertFrom-Json
    $formData2 = $formData | ConvertFrom-Json
    
    #Load the page once to get the csrf token
    Invoke-RestMethod -Uri $uri -SessionVariable mySession -Method Get -OutFile test2.html
    
    #Parse the token
    $body.csrfmiddlewaretoken = $mySession.Cookies.GetCookies("https://bitbucket.org")[0].Value
    $formData2.csrfmiddlewaretoken = $mySession.Cookies.GetCookies("https://bitbucket.org")[0].Value
    #Load the token into the object
    $json = $body | ConvertTo-Json
    $formData = $formData2 | ConvertTo-Json
    
    #build the uri data needed by bb
    $uriData = "username=" + $body.username + "&password=" + $body.password + "&submit=&next=" + $body.next + "&csrfmiddlewaretoken=" + $body.csrfmiddlewaretoken
    
    #Sign into BB now
    Invoke-RestMethod -Uri "https://bitbucket.org/account/signin/" -WebSession $mySession -Body $uriData -Method Post -OutFile test.html -Headers $headers
    
    #Update referer to the downloads page
    $headers.referer = $baseuri + $page
    $fulluri = $baseuri + $page
    
    #Upload the file
    Invoke-RestMethod -Uri "$fulluri" -Method Post -OutFile test3.html -WebSession $mySession -Headers $headers -Body $formData -ContentType "multipart/form-data"
    

0 个答案:

没有答案