【问题标题】:Fetch and store JWT with Powershell使用 Powershell 获取和存储 JWT
【发布时间】:2019-05-15 21:02:13
【问题描述】:

我正在使用 REST 调用获取 JWT 进行身份验证。我是脚本新手,但我设法通过以下方式获取令牌:

$params = @{"@type"="login";
 "username"="username";
 "password"="password"; 
}
Invoke-WebRequest -Uri http://[SERVER]:[PORT]/api/jwt/login -Method POST -Body $params

但是如何将包含令牌的响应内容保存到我以后可以在标头中使用以供以后调用的参数?

兄弟, 帕特里克

【问题讨论】:

  • 您可以像在任何其他编程和脚本语言中一样保存内容:通过将其粘贴在变量中(就像您对变量 $params 所做的那样)

标签: rest api powershell jwt token


【解决方案1】:

首先你必须保存响应:

$res = Invoke-WebRequest -Uri http://[SERVER]:[PORT]/api/jwt/login -Method POST -Body $params

然后您可以通过键入$res 来检查响应包含的内容。您的访问令牌可能可以通过以下方式访问:

($res.Content | ConvertFrom-Json).access_token

$res.Content 从您获得的响应中获取实际内容。然后您可能想要转换它(通常从示例中的 JSON)并使用 .property_name 访问特定属性。

作为替代方案,您可以尝试使用Invoke-RestMethod,它将内容作为对象(请记住,它的行为可能会有所不同depending how the service handles authentication)。

【讨论】:

  • 谢谢!看起来你在这里做点什么。现在我得到 ConvertFrom-Json : Invalid JSON-primitiv: eyJhbGci0i...(token)............. AT C:\getJWT.ps1:13 char 22 + ($response.Content | ConvertFrom-Json).access_token + ~~~~~~~~~~~~~~ +CategoryInfo : NotSpecified: (:) [ConvertFrom-Json9, ArgumentException +FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonComman
  • 这就是我在实际代码之前写something like 的原因。我不知道你得到的响应的结构是什么。如果你想让我看看你必须在你的问题中发布$res.Content的结构(不在评论中)
猜你喜欢
  • 2014-10-15
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2019-11-25
相关资源
最近更新 更多