Translating Java code to Jython

How do I convert this bit of Java into Jython:

class ClientCredentialGrant {

    private final static String CLIENT_ID = "";
    private final static String AUTHORITY = "https://login.microsoftonline.com/<tenant>/";
    private final static String CLIENT_SECRET = "";
    private final static Set<String> SCOPE = Collections.singleton("");

    public static void main(String args[]) throws Exception {
        IAuthenticationResult result = acquireToken();
        System.out.println("Access token: " + result.accessToken());
    }

Python is a scripting language, so you don’t need a main function to run code, you can put that code directly in the script.

Next, Python has dynamic typing, so you don’t need to define the types.

The main function just becomes this:

result = acquireToken()
print "Access token: " + result.accessToken()

I don’t see the aquireToken definition or import, so I don’t know where it’s coming from. And I don’t see where the static fields are used either. You could put those in a python class, but it seems a bit silly. Perhaps it’s easier to define them globally?

CLIENT_ID = ""
AUTHORITY = "https://login.microsoftonline.com/<tenant>/"
CLIENT_SECRET = ""
SCOPE = [""]

There’s also no notion of private variables in Python.

@Sanderd17, thanks for your response; much appreciated.

This is the rest of the code:

import com.microsoft.aad.msal4j.ClientCredentialFactory;
import com.microsoft.aad.msal4j.ClientCredentialParameters;
import com.microsoft.aad.msal4j.ConfidentialClientApplication;
import com.microsoft.aad.msal4j.IAuthenticationResult;
import com.microsoft.aad.msal4j.IClientCredential;
import com.microsoft.aad.msal4j.MsalException;
import com.microsoft.aad.msal4j.SilentParameters;

import java.util.Collections;
import java.util.Set;
class ClientCredentialGrant {

    private final static String CLIENT_ID = "";
    private final static String AUTHORITY = "https://login.microsoftonline.com/<tenant>/";
    private final static String CLIENT_SECRET = "";
    private final static Set<String> SCOPE = Collections.singleton("");

    public static void main(String args[]) throws Exception {
        IAuthenticationResult result = acquireToken();
        System.out.println("Access token: " + result.accessToken());
    }
private static IAuthenticationResult acquireToken() throws Exception {

        // Load token cache from file and initialize token cache aspect. The token cache will have
        // dummy data, so the acquireTokenSilently call will fail.
        TokenCacheAspect tokenCacheAspect = new TokenCacheAspect("sample_cache.json");

        // This is the secret that is created in the Azure portal when registering the application
        IClientCredential credential = ClientCredentialFactory.createFromSecret(CLIENT_SECRET);
        ConfidentialClientApplication cca =
                ConfidentialClientApplication
                        .builder(CLIENT_ID, credential)
                        .authority(AUTHORITY)
                        .setTokenCacheAccessAspect(tokenCacheAspect)
                        .build();

        IAuthenticationResult result;
        try {
            SilentParameters silentParameters =
                    SilentParameters
                            .builder(SCOPE)
                            .build();

            // try to acquire token silently. This call will fail since the token cache does not
            // have a token for the application you are requesting an access token for
            result = cca.acquireTokenSilently(silentParameters).join();
        } catch (Exception ex) {
            if (ex.getCause() instanceof MsalException) {

                ClientCredentialParameters parameters =
                        ClientCredentialParameters
                                .builder(SCOPE)
                                .build();

                // Try to acquire a token. If successful, you should see
                // the token information printed out to console
                result = cca.acquireToken(parameters).join();
            } else {
                // Handle other exceptions accordingly
                throw ex;
            }
        }
        return result;
    }
}

Source: Link

So it's using external libraries. In that case, you need to make sure these libraries are available in Ignition. The best way of doing this is by creating your own module.

But you can also hack around it by putting it in the Ignition/lib/core directory under your install. This hack is however not supported by IA, and can break at any point.

Once you got that library loaded, please tell my which lines you have issues with. Most changes seem pretty straightforward as given in the first example.

2 Likes