From a9e876f16cb7bc7143fddd2bec54f3406f4acdea Mon Sep 17 00:00:00 2001 From: Nilesh Choudhary Date: Tue, 26 Nov 2024 16:13:01 +0000 Subject: [PATCH] Updated README about the Managed identity client usage --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index b90801c6..3d6ccce2 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,28 @@ Acquiring tokens with MSAL Go follows this general pattern. There might be some } confidentialClient, err := confidential.New("https://login.microsoftonline.com/your_tenant", "client_id", cred) ``` + * Initializing a Managed Identity client for SystemAssigned: + + ```go + import mi "github.com/AzureAD/microsoft-authentication-library-for-go/apps/managedidentity" + + // Managed identity client have a type of ID required, SystemAssigned or UserAssigned + miSystemAssigned, err := mi.New(mi.SystemAssigned()) + if err != nil { + // TODO: handle error + } + ``` + * Initializing a Managed Identity client for UserAssigned: + + ```go + import mi "github.com/AzureAD/microsoft-authentication-library-for-go/apps/managedidentity" + + // Managed identity client have a type of ID required, SystemAssigned or UserAssigned + miSystemAssigned, err := mi.New(mi.UserAssignedClientID("YOUR_CLIENT_ID")) + if err != nil { + // TODO: handle error + } + ``` 1. Call `AcquireTokenSilent()` to look for a cached token. If `AcquireTokenSilent()` returns an error, call another `AcquireToken...` method to authenticate. @@ -96,6 +118,16 @@ Acquiring tokens with MSAL Go follows this general pattern. There might be some accessToken := result.AccessToken ``` + * ManagedIdentity clietn can simply call `AcquireToken()`: + ```go + resource := "" + result, err := miSystemAssigned.AcquireToken(context.TODO(), resource) + if err != nil { + // TODO: handle error + } + accessToken := result.AccessToken + ``` + ## Community Help and Support We use [Stack Overflow](http://stackoverflow.com/questions/tagged/msal) to work with the community on supporting Azure Active Directory and its SDKs, including this one! We highly recommend you ask your questions on Stack Overflow (we're all on there!) Also browse existing issues to see if someone has had your question before. Please use the "msal" tag when asking your questions.