I'm trying to authenticate to the Visma Api. (visma.net) using OAuth2. So I've created an authhelper which uses the UserAgentClient to do this. All values in constants are correct. Of course this should be read from a file, but I just tried this for simplicity.
So the problem I'm facing is that in function Authenticate, ProcessUserAuthorization (I've tried both, hence why it's commented), returns null whereas the exchangefortoken one returns a 400 error.
What am I doing wrong, given that 1. All information provided is correct 2. The api works and authentication is supposed to work too since it does authenticate correctly in Postman. That leads me to believe I'm doing something wrong in the code.
public static class AuthHelper
{
public static UserAgentClient CreateClient()
{
var authorizationEndpoint = new Uri($"{Constants.IDENTITY_SERVER_ADDRESS}/connect/authorize");
var tokenEndpoint = new Uri($@"{Constants.IDENTITY_SERVER_ADDRESS}/connect/token");
var serverDesc = GetAuthorizationServerDescription();
var client = new UserAgentClient(serverDesc, Constants.CLIENT_ID, Constants.CLIENT_SECRET);
return client;
}
private static AuthorizationServerDescription GetAuthorizationServerDescription()
{
var authorizationEndpoint = $@"{Constants.IDENTITY_SERVER_ADDRESS}/connect/authorize";
var tokenEndpoint = $@"{Constants.IDENTITY_SERVER_ADDRESS}/connect/token";
var serverDesc = new AuthorizationServerDescription
{
ProtocolVersion = ProtocolVersion.V20,
AuthorizationEndpoint = new Uri(authorizationEndpoint),
TokenEndpoint = new Uri(tokenEndpoint)
};
return serverDesc;
}
}
private static void Authenticate()
{
var scopes = new List<string> { "offline_access", "ea:api", "ea:sales" };
var client = AuthHelper.CreateClient();
var c = client.ProcessUserAuthorization(new Uri(Constants.REDIRECT_URI), new AuthorizationState(scopes));
//var token = client.ExchangeUserCredentialForToken(Constants.USERNAME, Constants.PASSWORD, scopes);
Console.WriteLine(c.Callback.AbsolutePath);
}
Note that using ExchangeUserCredentialForToken returns a 400, and the ProcessUserAuthorization returns null. Eventually I just want a token, so I want to use ExchangeUserCredentialForToken