首页 > 解决方案 > 我需要一种在未经用户同意的情况下使用 ARM 或任何 powershell 对 sharepointonline api 连接进行身份验证的方法

问题描述

我希望自动化在 Azure 中未经用户同意的情况下对 sharepoint 在线 api 连接进行身份验证,因为我想在 azuredveops 管道中运行代码,并且我得到的代码确实打开了一个自定义表单并征求用户同意。如果我不同意,那就好必须通过 devops 管道运行它,但在我的情况下,是的,我需要通过代码运行授权,而无需用户/图形干预

我尝试了以下代码,该代码在我的本地运行良好,但正如我所解释的,它需要用户同意,这在 azuredevops 管道世界中不起作用

    [string] $ResourceGroupName = '*****',
    [string] $ResourceLocation = '******',
    [string] $api = 'office365',
    [string] $ConnectionName = 'SharepointOnline',
    [string] $subscriptionId = '*****'

)
 #OAuth window for user consent
    Function Show-OAuthWindow {
    Add-Type -AssemblyName System.Windows.Forms

    $form = New-Object -TypeName System.Windows.Forms.Form -Property @{Width=600;Height=800}
    $web  = New-Object -TypeName System.Windows.Forms.WebBrowser -Property @{Width=580;Height=780;Url=($url -f ($Scope -join "%20")) }
    $DocComp  = {
            $Global:uri = $web.Url.AbsoluteUri
            if ($Global:Uri -match "error=[^&]*|code=[^&]*") {$form.Close() }
    }
    $web.ScriptErrorsSuppressed = $true
    $web.Add_DocumentCompleted($DocComp)
    $form.Controls.Add($web)
    $form.Add_Shown({$form.Activate()})
    $form.ShowDialog() | Out-Null
    }

#login to get an access code 

#Login-AzureRmAccount 

#select the subscription
$ResourceLocation = (Get-AzureRmResource -ResourceGroupName CI | Select-Object Location)[0].Location

$subscription = Select-AzureRmSubscription -SubscriptionId $subscriptionId

#Get the connection and create if wasn't already created
$connection = Get-AzureRmResource -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -ResourceName $ConnectionName -ErrorAction SilentlyContinue

if(-not $connection) {
    $connection = New-AzureRmResource -Properties @{"api" = @{"id" = "subscriptions/" + $subscriptionId + "/providers/Microsoft.Web/locations/" + $ResourceLocation + "/managedApis/" + $api}; "displayName" = $ConnectionName; } -ResourceName $ConnectionName -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -Location $ResourceLocation -Force
}
#else get the connection
else{
$connection = Get-AzureRmResource -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -ResourceName $ConnectionName
}
Write-Host "connection status: " $connection.Properties.Statuses[0]

$parameters = @{
    "parameters" = ,@{
    "parameterName"= "token";
    "redirectUrl"= "https://online.microsoft.com/default/authredirect"
    }
}

#get the links needed for consent
$consentResponse = Invoke-AzureRmResourceAction -Action "listConsentLinks" -ResourceId $connection.ResourceId -Parameters $parameters -Force

$url = $consentResponse.Value.Link 

#prompt user to login and grab the code after auth
Show-OAuthWindow -URL $url

$regex = '(code=)(.*)$'
    $code  = ($uri | Select-string -pattern $regex).Matches[0].Groups[2].Value
    Write-output "Received an accessCode: $code"

if (-Not [string]::IsNullOrEmpty($code)) {
    $parameters = @{ }
    $parameters.Add("code", $code)
    # NOTE: errors ignored as this appears to error due to a null response

    #confirm the consent code
    Invoke-AzureRmResourceAction -Action "confirmConsentCode" -ResourceId $connection.ResourceId -Parameters $parameters -Force -ErrorAction Ignore
}

#retrieve the connection
$connection = Get-AzureRmResource -ResourceType "Microsoft.Web/connections" -ResourceGroupName $ResourceGroupName -ResourceName $ConnectionName
Write-Host "connection status now: " $connection.Properties.Statuses[0]

标签: oauth-2.0azure-devopssharepoint-onlineazure-logic-apps

解决方案


您必须首先使用凭据创建一个Automation帐户。AzureRunAs然后在您的自动化帐户中创建您的 Runbook。

关于 AzureRunAs 帐户 -文档

我的第一本 Runbook -文档

从 Azure Devops 我发现了这个可以帮助的博客


推荐阅读