首頁  >  文章  >  後端開發  >  php如何取得Azure Active Directory 令牌的實例詳解

php如何取得Azure Active Directory 令牌的實例詳解

黄舟
黄舟原創
2017-09-08 09:10:002273瀏覽

在呼叫 Azure Rest API 時,如果是屬於 Azure Resource Manager 的 API,則需要使用 Azure Active Directory (Azure AD)認證取得令牌(Token),然後才能夠進行存取。

以下是建立 Azure AD 應用,並授權其可以存取管理 Azure 的資源的步驟:

以取得更好的閱讀體驗也可以點擊此處。

Note

以下認證方式,只適用於 Azure Resource Manager 的 API。 即 endpoint 為 management.chinacloudapi.cn 的 API,不適用於 Azure Service Manager 的 API(endpoint 為 management.core.chinacloudapi.cn 的 API)。

登入Azure 帳號(PowerShell)



# # 

記錄取得到的TenantID 以供後續程式使用。

 

選擇目前訂閱ID

設定目前訂閱,多訂閱環境下需要執行該步驟:

Set-AzureRmContext -SubscriptionId <subscription ID>

 

建立AD 應用程式

查看新建立的應用程式對象,屬性ApplicationId,在後續會用來建立服務憑證,角色設定和Access Token。


$azureAdApplication = New-AzureRmADApplication -DisplayName "exampleapp" -HomePage "https://www.contoso.org" -IdentifierUris "https://www.contoso.org/example" -Password "<Your_Password>"

     
  1. 建立服務憑證

  2. Azure AD 應用程式建立服務憑證:
  3. New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
  4. 當建立完成服務憑證後,初始是沒有任何權限的,我們需要為其設定權限範圍。

 

授權

為您的服務憑證新增角色設置,在該範例中,為您的服務憑證設定存取您訂閱下所有資源的讀取權限。 如果想了解更多內容,請參考:Azure Role-based Access Control。

New-AzureRmRoleAssignment -RoleDefinitionName Contributor -ServicePrincipalName $azureAdApplication.ApplicationId
    其中
  1. RoleDefinitionName

    有三種權限設定:

  2. Reader 對Azure資源有讀取權限。

  3. Contributor 對Azure資源有管理權限,但無法授權他人。

Owner 對Azure資源擁有管理權限,也可以授權他人管理。

  

 

呼叫Oauth2 API 取得Token

這樣Azure AD Application 就創建完成了,我們可以使用以下三個信息,來獲取認證的Token。

telent-id 對應訂閱資訊上使用的 telentID。

application-id 建立應用程式傳回的 ApplicationID。

app password 建立應用程式時填入的密碼。

取得 Token 的方式,使用 Azure login oauth2 的認證接口,如果想了解更多,請參考此文件:Using the Azure Resource Manager REST API。 ######請參考以下程式碼:############
$tenlent_id = &#39;Your Sub Tenlent ID&#39;;
$client_id = &#39;Application ID&#39;;
$client_secret = &#39;Application Password&#39;;

$auth_url = &#39;https://login.chinacloudapi.cn/&#39;.$tenlent_id.&#39;/oauth2/token?api-version=1.0&#39;;
$auth = curl_init($auth_url);
$post_data= &#39;grant_type=client_credentials&resource=https://management.chinacloudapi.cn/&client_id=&#39;.$client_id.&#39;&client_secret=&#39;.urlencode($client_secret);

curl_setopt_array($auth, array(
CURLOPT_VERBOSE => 1,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array(
&#39;Content-Type: application/x-www-form-urlencoded&#39;
)
));
curl_exec($atuh);
echo "\n";
###執行查詢後會得到 Token 數據, access_token 即為存取 Token。 ############
{
"token_type": "Bearer",
"expires_in": "3600",
"expires_on": "1455680701",
"not_before": "1455676801",
"resource": "https://management.azure.com/",
"access_token": "eyJ0eXAiOi…"
}
###然後將您要存取的API 請求頭上加上Authorization 的Header 設置,並將其值設為:########## #####Token 之前要加上Bearer。 ###### ######呼叫範例:############
$token = &#39;eyJ0eXA…&#39;;
$host = &#39;management.chinacloudapi.cn&#39;;
$version = &#39;2015-09-01&#39;;
$url = &#39;https://&#39;.$host.&#39;/subscriptions/5bbf0cbb-647d-4bd8-b4e6-26629f109bd7/resourceGroups/Default-MySql-ChinaNorth/providers/Microsoft.MySql/servers/poddbtest/databases/kevintest?api-version=&#39;.$version;
$ch = curl_init($url);
$data = array(
&#39;properties&#39; => array(
&#39;charset&#39; => &#39;utf8&#39;,
&#39;collation&#39; => &#39;utf8_general_ci&#39;
),
);
$json = json_encode($data);

curl_setopt_array($ch, array(
CURLOPT_VERBOSE => 1,
CURLOPT_CUSTOMREQUEST => &#39;PUT&#39;,
CURLOPT_POSTFIELDS => $json,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => array(
&#39;Content-type:application/json&#39;,
&#39;Authorization:Bearer &#39;.$token
)
));

$ret =curl_exec($ch);
if (empty($ret)) {
    // some kind of an error happened
    echo &#39;Curl error: &#39; . curl_error($ch);
} else {
    $info = curl_getinfo($ch);
}
echo "\n";
### ####

以上是php如何取得Azure Active Directory 令牌的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn