Class AuthenticationUtil

java.lang.Object
com.azure.identity.AuthenticationUtil

public final class AuthenticationUtil extends Object
Utility methods for working with authentication.
  • Method Details

    • getBearerTokenSupplier

      public static Supplier<String> getBearerTokenSupplier(com.azure.core.credential.TokenCredential credential, String... scopes)
      Creates a Supplier that provides a Bearer token from the specified credential. The token is cached and will refresh when it expires.

      Using the supplier:

       DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
       String scope = "https://cognitiveservices.azure.com/.default";
       Supplier<String> supplier = AuthenticationUtil.getBearerTokenSupplier(credential, scope);
      
       // This example simply uses the Azure SDK HTTP library to demonstrate setting the header.
       // Use the token as is appropriate for your circumstances.
       HttpRequest request = new HttpRequest(HttpMethod.GET, "https://www.example.com");
       request.setHeader(HttpHeaderName.AUTHORIZATION, "Bearer " + supplier.get());
       HttpClient client = HttpClient.createDefault();
       client.sendSync(request, Context.NONE);
       
      Parameters:
      credential - The TokenCredential from which to retrieve a token.
      scopes - The scopes as appropriate for the token you are retrieving.
      Returns:
      A Supplier which returns the bearer token as a String.