1

I am trying to make a console app that consumes the EVE online API, it uses Auth0 verification for some API requests

Swagger details https://esi.tech.ccp.is/ui/ Auth0 url https://login.eveonline.com/oauth/authorize?

i am currently trying to login systematically however i keep getting the error

JsonReaderException Unexpected character encountered while parsing value: <. Path '', line 0, position 0.

I think i am doing the process wrong in the first place and that is just trying to pass the default json {"error":"invalid_request","error_description":"Some parameters are either missing or invalid"}

Could someone help me point out what i am missing please

private static void Main()
        {
            var t = Task.Run(() => LoginAsync());
            t.Wait();
        }

private static async Task LoginAsync()
        {
            const string URL = "https://login.eveonline.com/oauth/authorize?";
            AuthenticationApiClient client = new AuthenticationApiClient(new Uri(URL));

        var cred = new Auth0.AuthenticationApi.Models.AuthenticationRequest
        {
            ClientId = "abcdef",
            Username = "UN",
            Password = "PW",
            Scope = "esi-markets.structure_markets.v1",
            Connection = "Username-Password-Authentication"
        
        };
        Auth0.AuthenticationApi.Models.AuthenticationResponse response = await client.AuthenticateAsync(cred);            
    }

I am trying to retrieve the auth token so that I can do further requests with it. How can it be done?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Zarwalski
  • 69
  • 1
  • 9
  • Set the accept header to application/json. –  Apr 11 '18 at 14:33
  • how do i make the header in auth0. there is no client.header or client.accept in AuthenticationApiClient? – Zarwalski Apr 11 '18 at 14:50
  • I tried doing as suggested with the following – Zarwalski Apr 12 '18 at 11:10
  • I tried doing this by adding a custom handler AuthenticationApiClient client = new AuthenticationApiClient(new Uri(URL), new CustomMessageHandler()); public class CustomMessageHandler : HttpClientHandler { protected override Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); return base.SendAsync(request, cancellationToken); } } still no success. – Zarwalski Apr 12 '18 at 11:15

1 Answers1

0

You are trying to get an access token using implicit grant which is not correct. Implicit grant should be executed only in the browser (Javascript client). For non interactive client, you can implement Client credentials grant or resource owner password grant to get the access token. At first, create a Machine to Machine client and use that client credentials for token. The following code implements client credentials grant.

 class Program
    {
        private static string accessToken;
        private static async Task Main(string[] args)
        {
            await ClientCredentialsFlow();
        }

        protected static async Task ClientCredentialsFlow()
        {
            var body = new Model
            {
                grant_type = "client_credentials",
                client_id = "[client id]",
                client_secret = "[client secret]",
                audience = "[API Identifier]"
            };
            using (var client = new HttpClient())
            {
                var content = JsonConvert.SerializeObject(body);
                var stringContent = new StringContent(content, Encoding.UTF8, "application/json");
                var res = await client.PostAsync("https://[domain].auth0.com/oauth/token", stringContent);
                var responseBody = await res.Content.ReadAsStringAsync();
                var deserilizeBody = JsonConvert.DeserializeObject<AuthResponseModel>(responseBody);
                accessToken = deserilizeBody.access_token;
                Console.WriteLine(accessToken);

            }
        }

        internal class Model
        {

            public string grant_type { get; set; }
            public string client_id { get; set; }
            public string client_secret { get; set; }
            public string audience { get; set; }
        }

        internal class AuthResponseModel
        {
            public string access_token { get; set; }
            public string scopes { get; set; }
            public string expires_in { get; set; }
            public string token_type { get; set; }
        }

    }
}

To implement resource owner password grant, look at the following doc. https://auth0.com/docs/api-auth/tutorials/password-grant

Tanver Hasan
  • 1,687
  • 13
  • 12