通过传递凭据和 SessionToken 使用 Invoke-WebRequest 下载 JSON 文件

问题描述 投票:0回答:1

我正在尝试从需要用户名、密码、用户 ID 和 SessionToken 的 URL 下载 JSON 文件。我指的是一篇文章:https://support.tidepool.org/hc/en-us/articles/360029368992-Downloading-your-Tidepool-Web-Notes

PS C:\Users\rajiv\OneDrive\Documents\Personal\Diabetes> Get-Credential | Invoke-WebRequest -Uri https://api.tidepool.org/auth/login -Method Post

我收到以下错误。我很确定我通过了正确的凭据:

cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Invoke-WebRequest : {"code":400,"reason":"Missing id and/or password"}
At line:1 char:18
+ ... redential | Invoke-WebRequest -Uri https://api.tidepool.org/auth/logi ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

提前致谢。

powershell curl
1个回答
0
投票

您需要将凭证对象传递给

Invoke-WebRequest
-Credential
参数:

$credentials = Get-Credential

$response = Invoke-WebRequest -Uri https://api.tidepool.org/auth/login -Method Post -Credential $credentials

收到响应后,从响应标头中获取会话令牌,并从响应正文中的 json 文档中获取用户 ID:

$tokenHeader = @{
  'x-tidepool-session-token' = $response.Headers['x-tidepool-session-token']
}

$userId = ($response.Content |ConvertFrom-Json).userid

然后在后续请求中将新的

$tokenHeader
字典传递给
-Headers

$notesResponse = Invoke-WebRequest "https://api.tidepool.org/message/notes/$userId" -ContentType application/json -Headers $tokenHeader
© www.soinside.com 2019 - 2024. All rights reserved.