2012-01-19 106 views
20

我有基本的authentatication使用REST API捲曲工作:PowerShell的HTTP POST REST API基本驗證

curl -X POST -H 'Accept: application/json' -u user:password http://localhost/test/ 

但是,當我嘗試做使用PowerShell的WebRequest一樣,我得到403(拒絕)。 當我在REST代碼中禁用身份驗證檢查時,此腳本正常工作。

什麼是PowerShell來傳遞POST憑據的最佳方法要求相似捲曲或我能做些什麼來解決下面的腳本。

會很感激在這一些指導。謝謝。

這裏是我的PowerShell腳本:

function Execute-HTTPPostCommand() { 
    param(
     [string] $target = $null 
    ) 

    $username = "user" 
    $password = "pass" 

    $webRequest = [System.Net.WebRequest]::Create($target) 
    $webRequest.ContentType = "text/html" 
    $PostStr = [System.Text.Encoding]::UTF8.GetBytes($Post) 
    $webrequest.ContentLength = $PostStr.Length 
    $webRequest.ServicePoint.Expect100Continue = $false 
    $webRequest.Credentials = New-Object System.Net.NetworkCredential -ArgumentList $username, $password 

    $webRequest.PreAuthenticate = $true 
    $webRequest.Method = "POST" 

    $requestStream = $webRequest.GetRequestStream() 
    $requestStream.Write($PostStr, 0,$PostStr.length) 
    v$requestStream.Close() 

    [System.Net.WebResponse] $resp = $webRequest.GetResponse(); 
    $rs = $resp.GetResponseStream(); 
    [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs; 
    [string] $results = $sr.ReadToEnd(); 

    return $results; 

} 


$post = "volume=6001F930010310000195000200000000&arrayendpoint=2000001F930010A4&hostendpoint=100000051ED4469C&lun=2" 

$URL = "http://example.com/test/" 

Execute-HTTPPostCommand $URL 

回答

17

您的代碼看起來不錯,我會嘗試加入HTTP_AUTHORIZATION標題爲$ webrequest是這樣的:

$webRequest.Headers.Add("AUTHORIZATION", "Basic YTph"); 

其中YTph將是用戶名:密碼的base64編碼字符串。

+0

謝謝!這爲我做了。 – 2012-01-20 03:14:45

+0

您可以使用http://www.base64encode.org/獲取UTF-8 Base64字符串。 – SeriousM 2013-04-03 09:52:35

4

憑據屬性似乎是用於Windows身份驗證。嘗試使用此功能: Forcing Basic Authentication in WebRequest 我會建議你在任何情況下使用一些網絡調試,就像小提琴手,看嫋嫋的要求和您的要求之間的差異

16

我知道這是一個古老的線程,但對於那些誰可能碰到這個偶然的invoke-restmethod是進行API調用使用PowerShell一個更好,更簡單,車輛。

建立一個參數列表作爲一個哈希表:

$params = @{uri = 'https:/api.trello.com/1/TheRestOfYourURIpath'; 
        Method = 'Get'; #(or POST, or whatever) 
        Headers = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($acctname):$($password)")); 
      } #end headers hash table 
    } #end $params hash table 

$var = invoke-restmethod @params 

你的參數哈希表可能略有不同。

我居然還沒有獲得這樣的使用Trello工作,但我有GitHub上,小威業務經理和吉拉。

+0

所有人都注意到:對於大多數JIRA rest api遠程調用,「Invoke-RestMethod」適用於我。然而,我發現'Invoke-WebRequest'需要更新問題的修復版本和影響版本('fixVersions'和'versions',經過廣泛測試)。如果您超時,請考慮至少切換這兩個。這是我們的私人服務器JIRA中的問題,但不是我個人雲JIRA實例中的問題。 – bunkerdive 2015-11-12 14:47:10

0

這是我使用從合流下載頁面爲HTML文件的代碼。

$pageid = "176398584" ; 
$url = "http://wikiserver/wiki/pages/viewpage.action?pageId=$pageid" ; 
write-host "Establish credentials" ; 
$r = Invoke-WebRequest "http://wikiserver/wiki/pages/login.action" -SessionVariable my_session ; 
# $r ; 
$form = $r.Forms[1]; 
# $form ; 

# $c = $host.UI.PromptForCredential('Your Credentials', 'Enter Credentials', '', '') ; 
# $form.fields['os_username'] = $c.UserName ; 
# $form.fields['os_password'] = $c.GetNetworkCredential().Password ; 
$form.fields['os_username'] = "mywikirobotlogonname" ; 
$form.fields['os_password'] = "mywikirobotpassword" ; 
$form.fields['os_cookie']  = "true" ; 
$form.fields['os_destination'] = "%2Fpages%2Fviewpage.action%3FpageId%3D$pageid" ; 

$outputFile = "$pageid.html" ; 
$content = Invoke-WebRequest -Uri ($url + $form.Action) -WebSession $my_session -Method POST -Body $form.Fields ; 
$content.ParsedHTML.getElementById("content").innerHTML | Add-Content $outputFile 

主機用戶界面提示可用於要求用戶輸入他們的登錄信息。

取消註釋一個變量以顯示到系統輸出的形式,檢索到的頁面等內容,以troubleshoot- $ R $形式$內容