加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

Powershell – 使用Powershell对Azure AD应用程序执行“授予权限

发布时间:2020-12-14 04:26:23 所属栏目:Windows 来源:网络整理
导读:我正在使用AzureAD模块创建一个Azure AD应用程序来调用Microsoft Graph API.我可以成功生成访问令牌.但是,当我尝试调用API时,我有一个错误“消息”:“无效的范围声明/角色.”. 当我在Azure门户中创建的应用程序中单击“授予权限”按钮并重试对API的调用时,
我正在使用AzureAD模块创建一个Azure AD应用程序来调用Microsoft Graph API.我可以成功生成访问令牌.但是,当我尝试调用API时,我有一个错误“消息”:“无效的范围声明/角色.”.

当我在Azure门户中创建的应用程序中单击“授予权限”按钮并重试对API的调用时,调用正在运行.

我没有找到任何地方如何使用Powershell执行此“授予权限”操作.有没有办法做到这一点 ?

谢谢

达米安

解决方法

有一种简单的方法(作为管理员),它要求您为Powershell安装AzureAD和AzureRM模块,并且不受Microsoft支持.

我的博客的原始帖子/参考在这里:http://www.lieben.nu/liebensraum/2018/04/how-to-grant-oauth2-permissions-to-an-azure-ad-application-using-powershell-unattended-silently/

应该帮助您完成此任务的特定代码示例:

Function Grant-OAuth2PermissionsToApp{
Param(
    [Parameter(Mandatory=$true)]$Username,#global administrator username
    [Parameter(Mandatory=$true)]$Password,#global administrator password
    [Parameter(Mandatory=$true)]$azureAppId #application ID of the azure application you wish to admin-consent to
)

$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($Username,$secpasswd)
$res = login-azurermaccount -Credential $mycreds
$context = Get-AzureRmContext
$tenantId = $context.Tenant.Id
$refreshToken = @($context.TokenCache.ReadItems() | where {$_.tenantId -eq $tenantId -and $_.ExpiresOn -gt (Get-Date)})[0].RefreshToken
$body = "grant_type=refresh_token&refresh_token=$($refreshToken)&resource=74658136-14ec-4630-ad9b-26e160ff0fc6"
$apiToken = Invoke-RestMethod "https://login.windows.net/$tenantId/oauth2/token" -Method POST -Body $body -ContentType 'application/x-www-form-urlencoded'
$header = @{
'Authorization' = 'Bearer ' + $apiToken.access_token
'X-Requested-With'= 'XMLHttpRequest'
'x-ms-client-request-id'= [guid]::NewGuid()
'x-ms-correlation-id' = [guid]::NewGuid()}
$url = "https://main.iam.ad.ext.azure.com/api/RegisteredApplications/$azureAppId/Consent?onBehalfOfAll=true"
Invoke-RestMethod -Uri $url -Headers $header -Method POST -ErrorAction Stop
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读