AuthenticationUtil Class
- java.
lang. Object - com.
azure. identity. AuthenticationUtil
- com.
public final class AuthenticationUtil
Utility methods for working with authentication.
Method Summary
Modifier and Type | Method and Description |
---|---|
static Supplier<String> |
getBearerTokenSupplier(TokenCredential credential, String[] scopes)
Creates a Supplier that provides a Bearer token from the specified credential. |
Methods inherited from java.lang.Object
Method Details
getBearerTokenSupplier
public static Supplier
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:
Returns: