gpt4 book ai didi

azure - 通过 Powershell 将新的重定向 URI 插入 Azure 应用程序注册

转载 作者:行者123 更新时间:2023-12-03 07:10:16 26 4
gpt4 key购买 nike

我有一个 powershell 脚本,可以通过 powershell 插入新的重定向 URI。如果我一一运行命令,它会起作用,而如果我使用参数运行脚本,它会不起作用。请帮忙。

这是我的脚本。

#######################################################################################
#Create New Redirect URI in Azure App Service:
#######################################################################################
param
(
[string] $url,
[string] $objectId,
[string] $clientId,
[string] $tenantValue,
[string] $clientSecret,
[string] $serviceAccountEmail,
[string] $serviceAccountPassword
)

$webServiceURL = $url
Write-Host "$webServiceURL"
Write-Host "Done creating the webServiceURL"

Write-Host "Convert password to Secure string"
$SecurePassword = ConvertTo-SecureString $serviceAccountPassword -AsPlainText -Force
Write-Host "Done converting password to Secure string"

$Credential = New-Object System.Management.Automation.PSCredential($serviceAccountEmail, $SecurePassword)

Write-Host "Logging in"
Login-AzAccount -Credential $Credential

$tid = (Get-AzTenant).Id

Write-Host "Getting token"
$tokenBody = @{
'tenant' = $tid
'client_id' = $clientId
'scope' = 'https://graph.microsoft.com/.default'
'client_secret' = $clientSecret
'grant_type' = 'client_credentials'
}

$Params = @{
'Uri' = "https://login.microsoftonline.com/$tid/oauth2/v2.0/token"
'Method' = 'Post'
'Body' = $tokenBody
'ContentType' = 'application/x-www-form-urlencoded'
}

$AuthResponse = Invoke-RestMethod @Params

$AuthResponse


$header = @{
'Content-Type' = 'application/json'
'Authorization' = "Bearer $($AuthResponse.access_token)"
}

$header

$redirectUris = (Invoke-RestMethod -Method Get -Uri "https://graph.microsoft.com/beta/applications/$objectId" -Headers $header).spa.redirectUris

if ($redirectUris -notcontains "$webServiceURL") {
$redirectUris += "$webServiceURL"
Write-Host "Adding $webServiceURL to redirect URIs";
}


$body = @{
'spa' = @{
'redirectUris' = $redirectUris
}
} | ConvertTo-Json

Invoke-RestMethod -Method Patch -Uri "https://graph.microsoft.com/beta/applications/$objectId" -Headers $header -Body $body

Write-Host "Were there errors? (If the next line is blank, then no!) $error"

这是我的屏幕截图,如果我逐步输入命令,它就会工作:

enter image description here

这是如果我将其作为参数发送并执行脚本文件时发生错误的屏幕截图。

enter image description here

我需要等待每个步骤得到解决然后继续吗?请指教。

最佳答案

我尝试通过提供错误的 clientId 来重现您的问题,该错误不属于我正在登录的租户的一部分。

enter image description here

因此,作为解决方案,您必须传递驻留在租户中的正确 clientId ,以便您的代码成功运行,如下所示。

enter image description here

更新

重现403禁止错误

enter image description here

解决方案:在应用程序权限下为 Microsoft Graph 添加了 API 权限 -> Application.ReadWrite.All

enter image description here

输出: enter image description here

关于azure - 通过 Powershell 将新的重定向 URI 插入 Azure 应用程序注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70581169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com