API调用的0auth2 PowerShell标头

时间:2019-01-11 21:33:10

标签: json api powershell oauth-2.0

我写了一个PS函数来即时获取一个新的Oauth2访问密钥,该密钥可以成功运行。我解析响应以仅获取新的访问密钥并将其存储在变量中。让我们调用该变量$ accesskey

我运行以下

Invoke-RestMethod "https://mysite.example.com/manage/v5/users" -Method GET -Headers @{"Authorization"="Bearer $accesskey"} -ContentType "application/json"

但是,我收到以下错误消息

Invoke-RestMethod : {"name":"Unauthorized","message":["Your request was made with invalid credentials."],"code":0,"status":401}

Function Get-APIHeader {


$Uri = "https://mysite.example.com/oauth2/token"

# Set body 
$Body = @{
    grant_type = 'password'
    username = 'user'
    password = 'password'
    client_id = 'test'
    client_secret = '0123456789abcdefghijk'
    scope = 'api'
    redirect_uri = 'https://mysite.example.com/'
}

    $APIResponse = Invoke-RestMethod -Method Post -Uri $Uri -Body $Body

   @{
    'Authorization' = 'Bearer ' + $APIResponse.access_token
}

}

我的问题是,我是否需要在标头中存储其他信息?为了首先从我的函数中获取访问密钥,我必须传递我的客户端ID /秘密和api用户名/密码信息,并且成功。

谢谢

0 个答案:

没有答案