PowerShell中的OAuth2.0授权代码流
我编写了下面的函数以弹出一个IE窗口来处理PowerShell中OAuth2.0授权代码流的用户身份验证,该功能可以正常工作,但当将其作为函数调用时,它不会停留在while循环中,等待IE窗口的URL更改并过滤掉OAuth2.0授权代码,然后关闭窗口 有没有办法让函数“打开”更长时间,并确保它等待IE窗口的URL更改 欢迎所有关于该功能的评论PowerShell中的OAuth2.0授权代码流,powershell,oauth-2.0,Powershell,Oauth 2.0,我编写了下面的函数以弹出一个IE窗口来处理PowerShell中OAuth2.0授权代码流的用户身份验证,该功能可以正常工作,但当将其作为函数调用时,它不会停留在while循环中,等待IE窗口的URL更改并过滤掉OAuth2.0授权代码,然后关闭窗口 有没有办法让函数“打开”更长时间,并确保它等待IE窗口的URL更改 欢迎所有关于该功能的评论 我猜函数不知道什么是$RedirectURI 您应该将其作为函数的第二个参数,或者(至少) 我更喜欢使用第二个参数,但如果您进行范围界定,您应该能够在函数
我猜函数不知道什么是
$RedirectURI
您应该将其作为函数的第二个参数,或者(至少)
我更喜欢使用第二个参数,但如果您进行范围界定,您应该能够在函数中使用它,并使用
$script:RedirectURI
以下示例实现了WebBrowser控件的功能,它允许您注册导航事件处理程序以捕获从授权服务器获得的授权代码
为什么此函数中未定义
$RedirectURI
?是否要求自动化IE?只有当您必须操作呈现的网站(例如,被JavaScript修改后的网站)的结果时,您才真正需要它。如果不需要,我建议使用Invoke-WebRequest
/Invoke-RestMethod
,因为它们是可靠的cmdlet。使用IE,您无法可靠地等待,因为网站完全加载时没有定义它。@AdminOfThings我是为PowerShell编写的,因此重定向URI是本机客户端的默认URI(之前是硬编码的)@Thomas不幸的是,授权代码流要求用户交互身份验证(至少一次)因此,我恐怕除了尝试将其自动化之外,别无选择way@Glenn$RedirectURI
不是自动变量,因此在调用函数时将其用作参数。我的假设是西奥和其他人都是对的。尝试在函数中写入$RedirectURI
,看看它返回的是什么,如上文所述,因为我是为PowerShell编写的,重定向URI是本机客户端的默认URI,并且是硬编码的earlier@Glenn是的,但是在函数外部硬编码的变量在函数内部没有意义。在函数中使用一个简单的Write Host$RedirectURI.ToString()
将揭示这一点。请调整您的函数并添加第二个参数。然后使用$URL
和RedirectURI
@Theo调用函数“函数外部硬编码的变量在函数内部没有意义”。我不会那么说。如果在函数定义之前定义该变量,则它工作良好:$a='bar';函数foo{$a};foo
打印bar
。偶数函数foo{$a}$a='bar';foo
打印条
,在这里我会有一些顾虑。@Thomas这要看情况而定。OP只是向我们展示他/她的功能,没有其他功能<代码>$RedirectURI可能在硬编码和调用函数之间被更改/清除。我们也不知道该函数是否在另一个脚本或模块中,以及如何在脚本中使用。。
function Show-OAuth2AuthCodeWindow {
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0, HelpMessage = "The OAuth2 authorization code URL pointing towards the oauth2/v2.0/authorize endpoint as documented here: https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-auth-code-flow")]
[System.Uri] $URL
)
try {
# create an Internet Explorer object to display the OAuth 2 authorization code browser window to authenticate
$InternetExplorer = New-Object -ComObject InternetExplorer.Application
$InternetExplorer.Width = "600"
$InternetExplorer.Height = "500"
$InternetExplorer.AddressBar = $false # disable the address bar
$InternetExplorer.ToolBar = $false # disable the tool bar
$InternetExplorer.StatusBar = $false # disable the status bar
# store the Console Window Handle (HWND) of the created Internet Explorer object
$InternetExplorerHWND = $InternetExplorer.HWND
# make the browser window visible and navigate to the OAuth2 authorization code URL supplied in the $URL parameter
$InternetExplorer.Navigate($URL)
# give Internet Explorer some time to start up
Start-Sleep -Seconds 1
# get the Internet Explorer window as application object
$InternetExplorerWindow = (New-Object -ComObject Shell.Application).Windows() | Where-Object {($_.LocationURL -match "(^https?://.+)") -and ($_.HWND -eq $InternetExplorerHWND)}
# wait for the URL of the Internet Explorer window to hold the OAuth2 authorization code after a successful authentication and close the window
while (($InternetExplorerWindow = (New-Object -ComObject Shell.Application).Windows() | Where-Object {($_.LocationURL -match "(^https?://.+)") -and ($_.HWND -eq $InternetExplorerHWND)})) {
Write-Host $InternetExplorerWindow.LocationURL
if (($InternetExplorerWindow.LocationURL).StartsWith($RedirectURI.ToString() + "?code=")) {
$OAuth2AuthCode = $InternetExplorerWindow.LocationURL
$OAuth2AuthCode = $OAuth2AuthCode -replace (".*code=") -replace ("&.*")
$InternetExplorerWindow.Quit()
}
}
# return the OAuth2 Authorization Code
return $OAuth2AuthCode
}
catch {
Write-Host -ForegroundColor Red "Could not create a browser window for the OAuth2 authentication"
}
}