【问题标题】:Azure Websites Kudu REST API - AuthenticationAzure 网站 Kudu REST API - 身份验证
【发布时间】:2014-12-12 12:08:14
【问题描述】:

我正在尝试使用 PowerShell 通过 REST API 将更新的内容文件放到 Azure 网站上。但是,当将我的凭据提供给 Invoke-RestMethod -Credentials 时,我会返回标准 Azure 登录页面的 HTML。

如何通过 PowerShell 对 Kudu 进行身份验证?谢谢。

【问题讨论】:

    标签: rest powershell azure-web-app-service kudu


    【解决方案1】:

    您可以先通过 Powershell 获取网站,然后使用网站的发布凭据调用 Kudu REST API。下面的例子会得到 Kudu 版本。

    $website = Get-AzureWebsite -Name "WebsiteName"
    
    $username = $website.PublishingUsername
    $password = $website.PublishingPassword
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    
    $apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"
    
    $kuduVersion = Invoke-RestMethod -Uri "$apiBaseUrl/environment" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET
    

    【讨论】:

    • 基本身份验证的优秀示例和演示。
    • 我添加了一个新答案,以将 @Seth 的答案带入 Azure ARM 世界。
    • 小心,如果您使用的是 slot,$website.Name 将是 websitename(slot) 在这种情况下,Invoke-RestMethod 会失败。而是做$matchedNames = $azureWebSite.EnabledHostNames -match 'scm' if($matchedNames -and $matchedNames.count -gt 0) { $WebSiteName = $matchedNames[0] }
    【解决方案2】:

    在新的 ARM 世界和最新的 PowerShell 中,您需要对 @Seth 的回答进行一些调整。

    具体来说,获取发布凭据的方式不一样,就是前3行。其余的我无耻地从@Seth 复制以完成sn-p。

    确保根据需要替换 YourResourceGroup/YourWebApp:

    $creds = Invoke-AzureRmResourceAction -ResourceGroupName YourResourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName YourWebApp/publishingcredentials -Action list -ApiVersion 2015-08-01 -Force
    
    $username = $creds.Properties.PublishingUserName
    $password = $creds.Properties.PublishingPassword
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
    
    $apiBaseUrl = "https://$($website.Name).scm.azurewebsites.net/api"
    
    $kuduVersion = Invoke-RestMethod -Uri "$apiBaseUrl/environment" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method GET
    

    【讨论】:

    • 我正在尝试通过上面的 cmdlet 获取凭据,但使用的是 ResourceId,但我总是遇到错误。我所做的是运行您的命令,获取正确的凭据,然后使用 Id 作为 -ResourceId 来查看是否可以获得它们(我需要为不同资源组中的一系列网站自动执行此操作)。知道为什么这不起作用吗?
    • 对不起,我没有完全按照。最好提出一个新问题,以便您可以更详细地展示您正在尝试的内容。在这里发布新的问题链接,我会尝试看看。
    • 问题是here,谢谢!
    猜你喜欢
    • 2021-07-27
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 2018-07-23
    • 2019-04-28
    相关资源
    最近更新 更多