@amohan123
The AzureROPCTokenProvider
is an OAuth 2.0 Resource Owner Password Credentials provider, described in more detail on Microsoft website:
Could you please check “EWS.AccessAsUser.All
” permission? The “Sign in to Outlook” scope should be used:
API Permissions.png (65.3 KB)
ITokenProvider provider = new AzureROPCTokenProvider(oauth2.Tenant, oauth2.ClientId, "", oauth2.userNameEmail, oauth2.userPassword,
new String[] { "https://outlook.office.com/EWS.AccessAsUser.All" });
NetworkCredential credentials = new OAuthNetworkCredential(oauth2.userNameEmail, provider);
Also, we have prepared 22.9.2 release, to check Oauth2 Impersonation issue:
You need to disable OAUTH Impersonation in the test:
EWSClient.useOAUTHImpersonation(false);
Moreover, you can also use MSAL Java lib to acquire Token:
https://mvnrepository.com/artifact/com.microsoft.azure/msal4j/1.13.2
Sample from Microsoft website:
private static IAuthenticationResult acquireTokenUsernamePassword(PublicClientApplication pca,
Set<String> scope,
IAccount account,
String username,
String password) throws Exception {
IAuthenticationResult result;
try {
SilentParameters silentParameters =
SilentParameters
.builder(scope)
.account(account)
.build();
// Try to acquire token silently. This will fail on the first acquireTokenUsernamePassword() call
// because the token cache does not have any data for the user you are trying to acquire a token for
result = pca.acquireTokenSilently(silentParameters).join();
System.out.println("==acquireTokenSilently call succeeded");
} catch (Exception ex) {
if (ex.getCause() instanceof MsalException) {
System.out.println("==acquireTokenSilently call failed: " + ex.getCause());
UserNamePasswordParameters parameters =
UserNamePasswordParameters
.builder(scope, username, password.toCharArray())
.build();
// Try to acquire a token via username/password. If successful, you should see
// the token and account information printed out to console
result = pca.acquireToken(parameters).join();
System.out.println("==username/password flow succeeded");
} else {
// Handle other exceptions accordingly
throw ex;
}
}
return result;
}
public static String getAccessToken() throws Exception {
Set<String> scope = new HashSet<String>();
scope.add("https://outlook.office365.com/.default");
String username = "test1@onmicrosoft.com";
String password = "userPass";
String clientId = "xxxxxb-f4be-4e2e-95dd-7aa4f5dxxxxx";
String tenantId = "xxxxx65f-f7e3-4bc3-841f-13b29xxxxx";
String authority =
"https://login.microsoftonline.com/" + tenantId + "/oauth2/v2.0/token";
PublicClientApplication pca = PublicClientApplication.builder(clientId)
.authority(authority)
.build();
//Get list of accounts from the application's token cache, and search them for the configured username
//getAccounts() will be empty on this first call, as accounts are added to the cache when acquiring a token
Set<IAccount> accountsInCache = pca.getAccounts().join();
IAccount account = getAccountByUsername(accountsInCache, username);
//Attempt to acquire token when user's account is not in the application's token cache
IAuthenticationResult result = acquireTokenUsernamePassword(pca, scope, account, username, password);
System.out.println("Account username: " + result.account().username());
System.out.println("Access token: " + result.accessToken());
System.out.println("Id token: " + result.idToken());
System.out.println();
accountsInCache = pca.getAccounts().join();
account = getAccountByUsername(accountsInCache, username);
//Attempt to acquire token again, now that the user's account and a token are in the application's token cache
result = acquireTokenUsernamePassword(pca, scope, account, username, password);
System.out.println("Account username: " + result.account().username());
System.out.println("Access token: " + result.accessToken());
System.out.println("Id token: " + result.idToken());
return "";
}
private static IAccount getAccountByUsername(final Set<IAccount> accountsInCache, final String username) {
if (accountsInCache.isEmpty()) {
System.out.println("==No accounts in cache");
} else {
System.out.println("==Accounts in cache: " + accountsInCache.size());
for (IAccount account : accountsInCache) {
if (account.username().equals(username)) {
return account;
}
}
}
return null;
}