2017-08-02 29 views
0

我想發佈一個文件到倉庫到位桶的「下載」部分工作。我正在關注Bitbucket文檔中的this頁面。我試圖通過編寫一個Powershell腳本來做到這一點。我已經成功地得到了GET方法一起工作:無法獲取POST與PowerShell和到位桶

#Invoke-RestMethod -Credential $cred -Uri https://api.bitbucket.org/2.0/repositories/myBitbucketUsername/myBitbucketRepoName/downloads?access_token=9EtOz9JDeWGwxTLKx3ya2oPE8g652GoLN0cMmtD0Ncvkf2OXoio0bcXwSigNE9AXTT2aj6qmbS5XHae7rIc%3D"&"scopes=pipeline%3Avariable+webhook+snippet%3Awrite+wiki+issue%3Awrite+pullrequest%3Awrite+repository%3Adelete+repository%3Aadmin+project%3Awrite+team%3Awrite+account%3Awrite"&"expires_in=3600 -OutFile .\file.txt 

爲了得到那個去上班我不得不添加訪問令牌,我通過到位桶(因此所有文本的訪問令牌部分構成的Uri)。

我不知道如何執行POST。我已經試過幾件事,包括類似文件的東西:

curl -s -u myBitbucketUsername -X POST https://api.bitbucket.org/2.0/repositories/myBitbucketUsername/myBitbucketRepoName/downloads -F [email protected] 

運行此命令時,我進去PowerShell中的錯誤是:

Invoke-WebRequest : Parameter cannot be processed because the parameter name 'u' is ambiguous. Possible matches include: 
-UseBasicParsing -Uri -UseDefaultCredentials -UserAgent. 
At C:\Users\user\Desktop\ScriptTest.ps1:21 char:9 
+ curl -s -u myBitbucketUsername-X POST https://api.bitbucket.org/2.0/reposit ... 
+   ~~ 
    + CategoryInfo   : InvalidArgument: (:) [Invoke-WebRequest], ParameterBindingException 
    + FullyQualifiedErrorId : AmbiguousParameter,Microsoft.PowerShell.Commands.InvokeWebRequestCommand 

我也試着與添加到訪問令牌URL(類似於我如何進行GET)。

我想POST的文件是「hello.txt的」,這是對我的桌面 - 相同的位置,我的PowerShell腳本。

任何想法?

編輯: 我遵循briantist的建議,並改用憑據代替。現在,我收到'Bad Request (400)'錯誤。這裏是我的代碼:

$postParams = @{files='.\Hello.txt'} 
Invoke-WebRequest -Credential $cred -Uri https://api.bitbucket.org/2.0/repositories/myBitbucketUsername/myRepoName/downloads -ContentType "multipart/form-data" -Method POST -Body $postParams 
+0

'捲曲-s -u myBitbucketUsername-X POST'你知道有應該是'myBitbucketUsername'和'-X'之間的空格嗎? – Milk

+0

@Milk oops - 這是從複製到帖子中的錯字。我已更新該帖子。 – Roka545

回答

1

在PowerShell中curlInvoke-WebRequest別名(所以是wget)。這是一個錯誤;他們不應該這樣做,因爲參數是不一樣的,並Invoke-WebRequest,一邊做類似的東西,是不是一個簡易替換。

錯誤告訴你什麼是問題:-u對於Invoke-WebRequest不明確。它有3個可能的參數,它告訴你它們是什麼。

但關鍵的是,你要使用它作爲一個用戶名,這Invoke-WebRequest不接受所有(它需要一個[PSCredential]對象,而不是一個單獨的明文用戶名和密碼)。

所以,你應該look at how to use credentials with Invoke-WebRequest,或直接使用curl,如果你有它,並且想要使用它。繞過別名

一種方法是用擴展叫它:curl.exe

另一種方法是刪除該別名:Remove-Alias -Name curl

+0

我已經切換到使用憑據,但現在我得到一個錯誤的請求(400)錯誤。我已將更多信息添加到原始帖子的底部。 – Roka545

+1

@ Roka545你不能只是'通過將文件名在體內POST'文件,[使用'-InFile'參數(https://stackoverflow.com/a/22563552/3905079)。 – briantist