Hi Team,

I am trying to generate access token by writing in JSR223 script.
I am using following code to generate access token:
String getBearerToken() {

// Scopes should be split into an array if they are in a single string

int maxBearerCount = 500;
    try {
        // Initialize SecretClient to retrieve the certificate from Azure Key 
Vault
        SecretClient secretClient = new SecretClientBuilder()
            .vaultUrl(keyVaultUrl)
            .credential(new DefaultAzureCredentialBuilder().build())
            .buildClient()

        // Retrieve and decode the certificate
        String certificate = secretClient.getSecret(certificateName).getValue()
        byte[] pfxData = Base64.getDecoder().decode(certificate)
        // Convert byte array to InputStream
        InputStream pfxStream = new ByteArrayInputStream(pfxData)

        // Create a credential using the certificate
        ClientCertificateCredential credential = new 
ClientCertificateCredentialBuilder()
            .tenantId(tenantId)
            .clientId(clientId)
            .pfxCertificate(pfxStream) // Use empty string for password if none
            .sendCertificateChain(true)
            .build()

        // Request a token
       // TokenRequestContext tokenRequestContext = new 
TokenRequestContext().setScopes([scope])
       // Create a TokenRequestContext and set the scopes
TokenRequestContext tokenRequestContext = new 
TokenRequestContext().setScopes([scope])
        log.info("Token request context: " + tokenRequestContext)

        AccessToken accessToken = 
credential.getToken(tokenRequestContext).block()


        // Return the token if available
        if (accessToken != null) {
         String bearerToken1 =accessToken.getToken()
             log.info("Token  " + bearerToken1)
            return bearerToken1
        } else {
            return null
        }
    } catch (Exception e) {
        log.error("Failed to get token: " + e.getMessage())
        return null
    }
}


// Save the token to a JMeter variable if needed
vars.put("bearerToken", getBearerToken())

I added all the required dependencies still I am getting following error, every 
time when I am running JMeter

2024-08-22 14:37:00,108 INFO o.a.j.t.JMeterThread: Thread finished: Thread 
Group 1-1
2024-08-22 14:37:00,108 ERROR o.a.j.JMeter: Uncaught exception in thread 
Thread[Thread Group 1-1,6,main]
java.lang.NoClassDefFoundError: com/nimbusds/common/contenttype/ContentType
              at 
com.nimbusds.oauth2.sdk.http.HTTPMessage.setContentType(HTTPMessage.java:104) 
~[oauth2-oidc-sdk-11.18.jar:11.18]
              at 
com.nimbusds.oauth2.sdk.http.HTTPRequest.setContentType(HTTPRequest.java:74) 
~[oauth2-oidc-sdk-11.18.jar:11.18]
              at 
com.microsoft.aad.msal4j.TokenRequestExecutor.createOauthHttpRequest(TokenRequestExecutor.java:44)
 ~[msal4j-1.16.2.jar:1.16.2]
              at 
com.microsoft.aad.msal4j.TokenRequestExecutor.executeTokenRequest(TokenRequestExecutor.java:34)
 ~[msal4j-1.16.2.jar:1.16.2]
              at 
com.microsoft.aad.msal4j.AbstractApplicationBase.acquireTokenCommon(AbstractApplicationBase.java:57)
 ~[msal4j-1.16.2.jar:1.16.2]
              at 
com.microsoft.aad.msal4j.AcquireTokenByAuthorizationGrantSupplier.execute(AcquireTokenByAuthorizationGrantSupplier.java:63)
 ~[msal4j-1.16.2.jar:1.16.2]
              at 
com.microsoft.aad.msal4j.AcquireTokenByClientCredentialSupplier.acquireTokenByClientCredential(AcquireTokenByClientCredentialSupplier.java:86)
 ~[msal4j-1.16.2.jar:1.16.2]
              at 
com.microsoft.aad.msal4j.AcquireTokenByClientCredentialSupplier.execute(AcquireTokenByClientCredentialSupplier.java:49)
 ~[msal4j-1.16.2.jar:1.16.2]

Could you please suggest how can I resolve this issue?

Regards,
Raghunandan

Reply via email to