connecting to outlook365 from selenium java

Using the Graph API one can access the Outlook.

  1. you need to register in Azure App.Usauly done by IT Team.you would get a client Id, Client secret and Tennant id.

  2. then you need to use Client credential autorization provider to get the tokken. below is the code from Microsoft Java SDK shared at https://github.com/Azure-Samples/ms-identity-java-daemon/blob/master/msal-client-credential-secret/src/main/java/ClientCredentialGrant.java

    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.nimbusds.oauth2.sdk.http.HTTPResponse;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Collections;
    import java.util.Properties;
    import java.util.concurrent.CompletableFuture;
    
    class ClientCredentialGrant {
    
        private static String authority;
        private static String clientId;
        private static String secret;
        private static String scope;
        private static ConfidentialClientApplication app;
    
        public static void main(String args[]) throws Exception{
    
            setUpSampleData();
    
            try {
                BuildConfidentialClientObject();
                IAuthenticationResult result = getAccessTokenByClientCredentialGrant();
                String usersListFromGraph = getUsersListFromGraph(result.accessToken());
    
                System.out.println("Users in the Tenant = " + usersListFromGraph);
                System.out.println("Press any key to exit ...");
                System.in.read();
    
            } catch(Exception ex){
                System.out.println("Oops! We have an exception of type - " + ex.getClass());
                System.out.println("Exception message - " + ex.getMessage());
                throw ex;
            }
        }
        private static void BuildConfidentialClientObject() throws Exception {
            
            // Load properties file and set properties used throughout the sample
            app = ConfidentialClientApplication.builder(
                    clientId,
                    ClientCredentialFactory.createFromSecret(secret))
                    .authority(authority)
                    .build();               
        }
    
        private static IAuthenticationResult getAccessTokenByClientCredentialGrant() throws Exception {
            
            // With client credentials flows the scope is ALWAYS of the shape "resource/.default", as the
            // application permissions need to be set statically (in the portal), and then granted by a tenant administrator
            ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                    Collections.singleton(scope))
                    .build();
            
            CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);
            return future.get();
        }
    
        private static String getMessagesGraph(String accessToken) throws IOException {
            URL url = new URL("https://graph.microsoft.com/v1.0/users/<yourEmailID>//mailFolders/Inbox/messages?$search=\"<yourSubject>\"");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Authorization", "Bearer " + accessToken);
            conn.setRequestProperty("Accept","application/json");
    
            int httpResponseCode = conn.getResponseCode();
            if(httpResponseCode == HTTPResponse.SC_OK) {
    
                StringBuilder response;
                try(BufferedReader in = new BufferedReader(
                        new InputStreamReader(conn.getInputStream()))){
    
                    String inputLine;
                    response = new StringBuilder();
                    while (( inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                }
                return response.toString();
            } else {
                return String.format("Connection returned HTTP code: %s with message: %s",
                        httpResponseCode, conn.getResponseMessage());
            }
        }
    
        /**
         * Helper function unique to this sample setting. In a real application these wouldn't be so hardcoded, for example
         * different users may need different authority endpoints or scopes
         */
        private static void setUpSampleData() throws IOException {
            // Load properties file and set properties used throughout the sample
            Properties properties = new Properties();
            properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("application.properties"));
            authority = properties.getProperty("AUTHORITY");
            clientId = properties.getProperty("CLIENT_ID");
            secret = properties.getProperty("SECRET");
            scope = properties.getProperty("SCOPE");
        }
    }

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top