![](/img/trans.png)
[英]Invoke-RestMethod :unauthorized client for getting Authentication-token
[英]Invoke-RestMethod on an app requiring Azure authentication
如何使用Powershell在Azure身份验证后面调用API? 更具体地说,我确实希望此行能够正常工作: Invoke-RestMethod -Method Get -Uri 'https://contoso.com/api/Hello
我的API是本地的(但在Web上可见),并且配置了Azure AD身份验证并且是必需的。
我希望以编程方式通过Powershell进行身份验证,因此我可以调用API,而无需任何其他交互。
当前,无论执行什么操作时,我在执行Invoke-RestMethod
都Invoke-RestMethod
401错误。
关于API
我正在使用通过Visual Studio创建的示例asp.net核心c#应用程序(使用工作或学校身份验证方法)。 此过程在Azure应用程序注册中创建,我确实在Azure中手动创建了客户端机密。
我尝试过的
现在,使用Powershell,我完成了以下工作
$Params = @{
'client_id' = ''
'Redirect_URI' = ''
'TenantID' = ''
'response_type'='code'
}
$ClientSecret = ''
从那里,我确实获得了使用的授权代码:
$Query = "?"; $Params.Keys | % {$Query+= "$($_)=$($Params.Item($_))&"} ; $Query = $Query.TrimEnd('&')
$IE= new-object -ComObject "InternetExplorer.Application"
$IE.Visible = $true
$IE.navigate2("https://login.microsoftonline.com/$($params.TenantID)/oauth2/authorize$Query")
Write-Host 'Press enter when Code is displayed in URL'
pause
$Code = [System.Web.HttpUtility]::ParseQueryString(([uri]$ie.LocationURL).Query)['code']
$ie.quit()
最后,我使用以下命令获取访问令牌:
$TokenResult = Invoke-RestMethod -Method Post -ContentType 'application/x-www-form-urlencoded' -Uri "https://login.microsoftonline.com/$($Params.TenantID)/oauth2/v2.0/token" -Body @{
client_id = $Params.client_id
scope = 'User.Read offline_access'
code = $Code
redirect_uri = $Params.Redirect_URI
grant_type = 'authorization_code'
client_secret = $ClientSecret
}
但是,如果我现在尝试使用以下方法调用我的API:
Invoke-RestMethod 'https://SomeDomain.com/api/values' -Headers @{Authorization = "Bearer $($TokenResult.access_token)"}
结果
我收到未经授权的401
我的观察
我确实相信我已成功使用以下方法与Graph API和其他Azure API进行交互。 但是,这在调用我自己的Azure身份验证背后的API时不起作用。
如何进行适当的调用,使我可以使用Azure身份验证针对我的私有API进行身份验证?
我想您尚未在AAD中配置您的API。 若要通过您的AD应用访问API,还需要在门户中为您的API创建一个AD应用,请参阅: 使用Azure Active Directory v2.0终结点注册应用。 并将其配置为公开API,请参阅: 配置应用程序公开Web API(预览)和配置客户端应用程序访问Web API(预览) 。 然后,您将可以调用Graph API,Office 365 API等API。
您可以通过公开权限/范围和角色来开发Web API并将其提供给客户端应用程序。 与其他Microsoft Web API一样,提供了正确配置的Web API,包括Graph API和Office 365 API。
根据您的脚本,您需要将Header指定为Headers @{Authorization = "Bearer "+ $TokenResult.access_token}
。 我有一个样本。
$Params = @{
'client_id' = ''
'redirect_URI' = ''
'response_type'='code'
'scope' = 'https://graph.microsoft.com/.default offline_access'
}
$ClientSecret ='.C$S|NAYQ:q)ES2w(A)*]Xp5T-?Fx+[aKE+YtmcaX!u1!{>T*wVJ70h:Q2s'
$TeantID = 'e4c9ab4e-bd27-40d5-8459-230ba2a757fb'
$Query = "?"; $Params.Keys | % {$Query+= "$($_)=$($Params.Item($_))&"} ; $Query = $Query.TrimEnd('&')
$IE= new-object -ComObject "InternetExplorer.Application"
$IE.Visible = $true
$IE.navigate2("https://login.microsoftonline.com/$($TeantID)/oauth2/authorize$Query")
Write-Host 'Press enter when Code is displayed in URL'
pause
Add-Type -AssemblyName System.Web
$Code = [System.Web.HttpUtility]::ParseQueryString(([uri]$ie.LocationURL).Query)['code']
$ie.quit()
$TokenResult = Invoke-RestMethod -Method Post -ContentType 'application/x-www-form-urlencoded' -Uri "https://login.microsoftonline.com/$($TeantID)/oauth2/v2.0/token" -Body @{
client_id = $Params.client_id
scope = 'User.Read offline_access'
code = $Code
redirect_uri = $Params.Redirect_URI
grant_type = 'authorization_code'
client_secret = $ClientSecret
}
Invoke-RestMethod -Method Get -Uri 'https://graph.microsoft.com/v1.0/me' -Headers @{Authorization = "Bearer "+ $TokenResult.access_token}
有关更多详细信息,请参阅https://docs.microsoft.com/zh-cn/azure/active-directory/develop/v2-oauth2-auth-code-flow
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.