Can not convert Microsoft.Exchange.WebServices.Data.OAuthCredentials to System.Net.Icredential

client = oAuthType ? EWSClient.GetEWSClient(txtURL, (ICredentials)getTokenPublic()) : EWSClient.GetEWSClient(txtURL, txtUserName, txtPswd, txtDomain)
// this line i m getting error

private OAuthCredentials getTokenPublic()
        {
            var accessParam = new AccessParameters();
            if (loggingLevel >= (int)errlevel.msgInfo)
            {
                AddtoLog((int)errlevel.msgInfo, "In Method getTokenPublic");
            }
            if (!ValidateToken())
            {
                accessParam.ClientId = ((EwsConnectorProperties)Properties).ClientIdPublic;
                accessParam.TenantId = ((EwsConnectorProperties)Properties).TenantIdPublic;
                accessParam.ClientSecret = ((EwsConnectorProperties)Properties).ClientSecretPublic;
                TokenObject = GetAccessToken_APP(accessParam);
            }
            OAuthCredentials credentials;
            if (TokenObject.AccessToken is not null)
            {

                credentials = new OAuthCredentials(TokenObject.AccessToken); // New OAuthNetworkCredential(accessParam.UserName, TokenObject.AccessToken)
            }
            else
            {
                throw new Exception("Access Token is either null or empty");
            }

            return credentials;
        }

@sureshkumarg

It seems you are encountering an issue when trying to convert Microsoft.Exchange.WebServices.Data.OAuthCredentials to System.Net.ICredentials. The OAuthCredentials class is specifically designed for OAuth authentication, and it does not directly implement the ICredentials interface, which is likely causing the conversion error.

To resolve this, you can use the OAuthNetworkCredential class, which is designed to work with OAuth tokens and implements the ICredentials interface. Here’s how you can modify your code:

private OAuthCredentials getTokenPublic()
{
    var accessParam = new AccessParameters();
    if (loggingLevel >= (int)errlevel.msgInfo)
    {
        AddtoLog((int)errlevel.msgInfo, "In Method getTokenPublic");
    }
    if (!ValidateToken())
    {
        accessParam.ClientId = ((EwsConnectorProperties)Properties).ClientIdPublic;
        accessParam.TenantId = ((EwsConnectorProperties)Properties).TenantIdPublic;
        accessParam.ClientSecret = ((EwsConnectorProperties)Properties).ClientSecretPublic;
        TokenObject = GetAccessToken_APP(accessParam);
    }
    OAuthCredentials credentials;
    if (TokenObject.AccessToken is not null)
    {
        credentials = new OAuthCredentials(TokenObject.AccessToken);
    }
    else
    {
        throw new Exception("Access Token is either null or empty");
    }

    return credentials;
}

// Usage
var mailboxUri = "https://outlook.office365.com/ews/exchange.asmx";
ICredentials credentials = new OAuthNetworkCredential(TokenObject.AccessToken);
using (IEWSClient ewsClient = EWSClient.GetEWSClient(mailboxUri, credentials))
{
    // Your code to interact with the EWS client
}

In this code, replace the line where you create the ICredentials instance with OAuthNetworkCredential, passing the access token directly. This should allow you to authenticate successfully with the EWS client using OAuth.

If you need further assistance or have additional questions, feel free to ask!

same i am trying sharedFolder But Public Folder i am using OAuthCredentials

@sureshkumarg,

It appears that you are using Aspose.Email APIs, so I will be transferring your thread to the appropriate forum where a member of our Aspose.Email team will assess your issue and provide assistance accordingly soon.

@sureshkumarg,

Instead of casting OAuthCredentials to ICredentials, you should use OAuthNetworkCredential as EWSClient.GetEWSClient is designed to accept ICredentials.

Modify your getTokenPublic method to return OAuthNetworkCredential:

private OAuthNetworkCredential getTokenPublic()
{
    var accessParam = new AccessParameters();

    if (loggingLevel >= (int)errlevel.msgInfo)
    {
        AddtoLog((int)errlevel.msgInfo, "In Method getTokenPublic");
    }

    if (!ValidateToken())
    {
        accessParam.ClientId = ((EwsConnectorProperties)Properties).ClientIdPublic;
        accessParam.TenantId = ((EwsConnectorProperties)Properties).TenantIdPublic;
        accessParam.ClientSecret = ((EwsConnectorProperties)Properties).ClientSecretPublic;
        TokenObject = GetAccessToken_APP(accessParam);
    }

    if (TokenObject.AccessToken is not null)
    {
        return new OAuthNetworkCredential(TokenObject.AccessToken);
    }
    else
    {
        throw new Exception("Access Token is either null or empty");
    }
}