我有這樣的腳本,成功登錄到網站,並進行查詢:如何使此PowerShell Web請求正確保存會話?
$credential = Get-Credential
$postParams = @{username=$credential.UserName;password=$credential.GetNetworkCredential().password}
Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/sessions/?Login -Method POST -Body $postParams -SessionVariable cookie
$query = Invoke-WebRequest -Uri http://www.nexusmods.com/newvegas/mods/searchresults/? -WebSession $cookie
我所試圖做的是做類似的事情與其他網站,Trello。這是腳本:
$credential = Get-Credential
$postParams = @{method='password';'factors[user]'=$credential.UserName;'factors[password]'=$credential.GetNetworkCredential().password}
Invoke-WebRequest -Uri https://trello.com/1/authentication -Method POST -Body $postParams -SessionVariable cookie
$result = Invoke-WebRequest -uri https://trello.com/ -WebSession $cookie
然而,結果變量顯示一個頁面,如果用戶沒有登錄,所以我假設會話未正確保存。我怎樣才能解決這個問題?
你想用Trello做什麼?他們有一個API,比試圖通過他們的網頁登錄要容易得多。如果你讓我知道你想做什麼,我可以幫忙。 – FoxDeploy
我正在編寫使用API的PowerShell腳本,但它希望我不想將我的密鑰或任何令牌硬編碼到其中,也不希望強制任何使用該腳本的人必須記住這些事情。 所以我的目標是使用webrequests登錄到網站並獲取帳戶的API密鑰並生成一個令牌,以便使用API的腳本只需從CLI獲得憑據。 –
我寫了一個方法來使用他們的API本地執行此操作,請告訴我這是否有幫助。 – FoxDeploy