问题描述
我目前正在使用powershell进行一些基本的API REST测试。
但是,我在捕获特定的输出数据时遇到了问题
例如:
$bearer = Invoke-RestMethod -Method POST -Body $body -uri "https://api.yourwebsite.com/oauth/token"
Output:
access_token
------------
{longtokenhere}
但是与标题一起使用时
$header = @{Authorization = "Bearer "+$bearer}
Output is:
Name Value
---- -----
Authorization Bearer @{access_token={longtokenhere}}
我想知道如何删除“ @ {access_token =}”部分,以便仅使用{longtokehere}?
解决方法
我做了一些测试,结果证明我可以直接调出access_token输出:
当我做这部分的时候:
$bearer = Invoke-RestMethod -Method POST -Body $body -uri "https://api.yourwebsite.com/oauth/token"
Output:
access_token
------------
{longtokenhere}
我只是简单地使用了这个:
$bearer.access_token
我仅获得令牌的直接输出:
{longtokenhere}
我的最终命令对此起作用:
Invoke-RestMethod -Method GET -Header @{Authorization = "Bearer "+$bearer.access_token} -ContentType "application/json" -uri "https://api.yourwebsite.com/release/releaseID"