Azure ADサービスプリンシパルを作成してから、認証(JWT)トークンを取得する必要があります。以下のサンプルスクリプトは、PowerShellを使用してAzure ADサービスの原則を作成する方法を示しています。ウォークスルーの詳細については、https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/#authenticate-service-principal-with-password—powershellのガイダンスを参照してください。 create a service principal via the Azure portalも可能です。
$pwd = “[your-service-principle-password]”
$subscriptionId = “[your-azure-subscription-id]”
Login-AzureRmAccount
Select-AzureRmSubscription -SubscriptionId $subscriptionId
$azureAdApplication = New-AzureRmADApplication `
-DisplayName “ Demo Web name” `
-HomePage “https://localhost/webdemo” `
-IdentifierUris “https://localhost/webdemo” `
-Password $pwd
New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
New-AzureRmRoleAssignment -RoleDefinitionName Reader -ServicePrincipalName $azureAdApplication.ApplicationId
$subscription = Get-AzureRmSubscription -SubscriptionId $subscriptionId
$creds = Get-Credential -UserName $azureAdApplication.ApplicationId -Message “Please use your service principle credentials”
Login-AzureRmAccount -Credential $creds -ServicePrincipal -TenantI $subscription.TenantId
GoLangについて詳しくはありませんが、次の.NETコードを参照してアプリケーションのアクセストークンを取得することができます。
public static string GetAccessToken()
{
var authenticationContext = new AuthenticationContext("https://login.windows.net/{tenantId or tenant name}");
var credential = new ClientCredential(clientId: "{client id}", clientSecret: "{application password}");
var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential:credential);
if (result == null) {
throw new InvalidOperationException("Failed to obtain the JWT token");
}
string token = result.AccessToken;
return token;
}
私はGoLangで働いています。AzureのREST APIをネットワーク管理に使用しています。私はまだアクセストークンを取得する方法を混同しています。私はすべての手順を完了し、AADを作成し、役割/許可を与えました。しかし、アクセストークンを取得する方法については固執しています。コードからアクセストークンを取得する方法はありますか? – Baber
@Baber質問に対処するための回答を修正しました –
ありがとうございます。それは本当に私をたくさん助けました。私はもう助けが必要な場合私はあなたをもう一度キャッチします:) – Baber