問題描述
PowerShell 腳本調(diào)用Azure REST API, 但是所有的API都需要進(jìn)行權(quán)限驗(yàn)證匾委。要在請(qǐng)求的Header部分帶上Authorization參數(shù)惩系,并用來對(duì)List Resource Group接口進(jìn)行授權(quán)廉嚼,然后顯示Resource Group的Name药磺,Location,和ID ...
問題解答
第一步:在Azure AD中注冊(cè)應(yīng)用舟舒,該應(yīng)用表示執(zhí)行PowerShell Script的客戶端擁有訪問Subscription下資源的權(quán)限拉庶。如無,則會(huì)出現(xiàn) AuthorizationFailed 錯(cuò)誤秃励,詳見附錄一:權(quán)限問題
在瀏覽器上的新標(biāo)簽頁中打開 Azure 門戶氏仗。
導(dǎo)航到“應(yīng)用注冊(cè)”以在 Active Directory 中注冊(cè)應(yīng)用。
選擇“新注冊(cè)”夺鲜。 在“注冊(cè)應(yīng)用程序”頁上皆尔,將值設(shè)置如下:
- 將“名稱”設(shè)置為一個(gè)有意義的名稱。 例如谣旁,powershell-client
- 將“支持的帳戶類型”設(shè)置為“僅限此組織目錄中的帳戶”床佳。
- 選擇“注冊(cè)” 。
注冊(cè)應(yīng)用程序之后,從“概述”頁復(fù)制“應(yīng)用程序(客戶端) ID” 和 “ 目錄(tenant) ID ”你稚。
在邊側(cè)菜單的“管理”部分下灸异,選擇“證書和機(jī)密” 。
在“證書和機(jī)密”頁中灼舍,選擇“客戶端機(jī)密”下的“新建客戶端機(jī)密”按鈕 。
- 輸入“說明”。
- 為“過期”選擇任一選項(xiàng)饼问。
- 選擇“添加” 。
- 在離開頁面之前復(fù)制客戶端的“機(jī)密 ID”揭斧。 稍后腳本中需要用到此值莱革。
第二步:在下面腳本中替換自己的 tenantId****, ****applicationId****讹开,和secret**
#===============================================
# 2021-11-14 通過Azure AD中的注冊(cè)應(yīng)用獲取Access Token
#
# Azure AD App Registrations: https://portal.azure.cn/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps
# 【Azure Developer】使用Postman獲取Azure AD中注冊(cè)應(yīng)用程序的授權(quán)Token盅视,及為Azure REST API設(shè)置Authorization: https://www.cnblogs.com/lulight/p/14279338.html
#===============================================
$tenantId='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$applicationId='xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$secret='-xxxx~xxxxxxxxxxxxxxxxxxxxxxxx'
$param = @{
Uri ="https://login.chinacloudapi.cn/$tenantId/oauth2/token";
Method = 'Post';
Body = @{
grant_type = 'client_credentials';
resource = 'https://management.chinacloudapi.cn';
client_id = $applicationId;
client_secret = $secret
}
}
Write-Host '調(diào)用Token接口 .. ' -ForegroundColor DarkYellow
$result = Invoke-RestMethod @param
$result
#===============================================
#
# 使用Token作為Authorization,調(diào)用Resource Groups - List: https://docs.microsoft.com/en-us/rest/api/resources/resource-groups/list
#
#===============================================
$subscriptionId = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
$token = $result.access_token
$param_rgList = @{
Uri = "https://management.chinacloudapi.cn/subscriptions/$subscriptionId/resourcegroups?api-version=2020-06-01";
ContentType = 'application/json';
Method = 'GET'
Headers = @{
Authorization = "Bearer $token";
host = "management.chinacloudapi.cn"
}
}
Write-Host '調(diào)用 Resource Groups - List 接口 .. ' -ForegroundColor DarkYellow
$rgList = Invoke-RestMethod @param_rgList
$rgList.value | Select-Object name, location, id
執(zhí)行結(jié)果:
附錄一:權(quán)限問題
錯(cuò)誤消息:
Invoke-RestMethod : {"error":{"code":"AuthorizationFailed","message":"The client '0b807bf1-40db-4e0c-888f-380b9b558cf1' with object id '0b807bf1-40db-4e0c-888f-380b9b558cf1' does not have authorization
to perform action 'Microsoft.Resources/subscriptions/resourcegroups/read' over scope '/subscriptions/a9dc7515-7692-4316-9ad4-762f383eec10' or the scope is invalid. If access was recently granted,
please refresh your credentials."}}
At line:49 char:12
+ $rgList = Invoke-RestMethod @param_rgList
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
錯(cuò)誤截圖:
解決辦法:
在門戶中進(jìn)入Subscriptions頁面旦万,對(duì)注冊(cè)應(yīng)用賦予Reader權(quán)限即可闹击。
1) 進(jìn)入Azure Subscriptions頁面:https://portal.azure.cn/#blade/Microsoft_Azure_Billing/SubscriptionsBlade
2) 選中訂閱,在Access Control(IAM)中對(duì)第一步中 AAD的注冊(cè)應(yīng)用賦予Reader權(quán)限成艘。
附錄二:PowerShell中URL后攜帶參數(shù)的‘赏半?’需要轉(zhuǎn)義
因?yàn)镻owerShell的變量名中后不能為符號(hào)贺归,如 ? 等,需要添加 ` 作為轉(zhuǎn)義字符(高亮部分)断箫,改為 '?拂酣。
如:
apiurl="https://management.chinacloudapi.cn/.../
apiurl="https://management.chinacloudapi.cn/.../CloudServiceName?api-version=2015-06-01"
應(yīng)修改為:
apiurl="https://management.chinacloudapi.cn/.../
apiurl="https://management.chinacloudapi.cn/.../CloudServiceName`?api-version=2015-06-01"
參考資料
Resource Groups - List:https://docs.microsoft.com/en-us/rest/api/resources/resource-groups/list
Postman獲取Azure AD中注冊(cè)應(yīng)用程序的授權(quán)Token,及為Azure REST API設(shè)置Authorization:https://www.cnblogs.com/lulight/p/14279338.html
當(dāng)在復(fù)雜的環(huán)境中面臨問題仲义,格物之道需:濁而靜之徐清踱葛,安以動(dòng)之徐生。 云中光坝,恰是如此!
分類: 【Azure 環(huán)境】
標(biāo)簽: Azure Developer, Azure 環(huán)境, PowerShell腳本調(diào)用獲取到AAD Token, 尸诽,然后Azure REST API如資源組列表