尝试获取令牌时,未找到auth上下文的AcquireTokenAsync

时间:2016-12-13 23:11:26

标签: c# powershell authentication oauth token

我试图让持有者令牌通过PowerShell脚本调用API,如下所示:

function GetAuthToken
{
    param
    (
        [Parameter(Mandatory=$true)]
        $ApiEndpointUri,

        [Parameter(Mandatory=$true)]
        $AADTenant
)
$adal = "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\" + `
            "Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
$adalforms = "C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\" + `
                "Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"

[System.Reflection.Assembly]::LoadFrom($adal) | Out-Null
[System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null

$clientId = "1950a258-227b-4e31-a9cf-717495945fc2"
$redirectUri = "urn:ietf:wg:oauth:2.0:oob"
$authorityUri = “https://login.windows.net/$aadTenant”

$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authorityUri



$authResult = $authContext.AcquireTokenAsync($ApiEndpointUri, $clientId,$redirectUri, "Auto")
$authResult.Wait();

return $authResult
$ApiEndpointUri = "https://management.azure.com/" #change this to graph api uri
$AADTenant = 'GUID' #AAD tenant guid
$token = GetAuthToken -ApiEndPointUri $ApiEndpointUri -AADTenant $AADTenant
$header = @{
'Content-Type'='application\json'
'Authorization'=$token.CreateAuthorizationHeader()
}

$request = ``
(Invoke-RestMethod -Uri $request -Headers $header -Method Get).value

但是,getstokenasync失败并出现此错误:

Cannot find an overload for "AcquireTokenAsync" and the argument count: "4".

为什么我会遇到这个问题的任何想法? AcquireTokenAsync从我所知道的内容中获取4个参数。

2 个答案:

答案 0 :(得分:0)

我没有看到带有4个参数的AcquireTokenAsync重载。您还需要确保正确地投射参数。

查看重载列表here。您认为自己使用了哪些重载?您会注意到所有参数都是字符串,但没有任何重载只接受字符串。

答案 1 :(得分:0)

我在Windows 10 build 1703上遇到了类似的错误(尽管脚本正在构建1607)。我安装了Microsoft.IdentityModel.Clients.ActiveDirectory版本3.13.9。我用version 3.13.8替换了那些程序集并重新启动了PowerShell,这清除了我的错误。