Can't get it to work: C# Console App Refreshing ESI Token

I could use some help please. I am trying to create a C# console app that gets an access key from a refresh token. Obviously my end program does far more, but I’m stuck here. I obtained the refresh key from postman, so this is my first attempt to use c# to interact with EvE ESI. Here is my code:

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static HttpClient client = new HttpClient();

        static async Task RunAsync()
        {
            string clientid = "<<client_id>>";
            string secret = "<<secret>>";
            string auth_header = $"{clientid}:{secret}";
            string auth_header_64 = $"Basic {Convert.ToBase64String(Encoding.UTF8.GetBytes(auth_header))}";
            string refreshtoken = "<<refresh Token>>";

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
            client.DefaultRequestHeaders.Clear();
            client.DefaultRequestHeaders.Add("Authorization", auth_header_64);

            HttpResponseMessage response;
            try 
            {
                response = await client.GetAsync($"https://login.eveonline.com/oauth/token?grant_type=refresh_token&refresh_token={refreshtoken}");
                if (!response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Error refreshing token");
                    Console.WriteLine(response.ToString());
                    return;
                }
                string data = await response.Content.ReadAsStringAsync();
                Console.WriteLine(data);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        static void Main(string[] args)
        {
            RunAsync().GetAwaiter().GetResult();
        }
    }
}

Here is the response that I am getting:

Error refreshing token
StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
  Cache-Control: private
  Server: Microsoft-IIS/10.0
  Date: Mon, 22 Apr 2019 03:19:24 GMT
  Content-Type: text/html
  Content-Length: 0
}

Thank you in advance for your help.

Are you sure you’re using the right ESI url? Last I was told it was esi.evetech.net, but you appear to be using login.eveonline.com, which is the URL for the SSO service rather than ESI.

I’m not sure why you’re getting the error, but, you’re if you’re not getting a json errors back, you’re most likely not sending everything you need to send or sending it to the wrong place, SSO should be returning json errors on failure.

I will also strongly suggest that you use a library instead of reinventing the wheel:

In case you insist on doing it yourself, then SSO is documented in the ESI docs:

1 Like

You can look here for a working example: https://github.com/panthernet/ThunderED/blob/1.3.8/ThunderED/API/ESIAPI.cs

Line 215 RefreshToken() method

1 Like

@ParaPants I don’t’ fully know C#, however it seems you are doing a GET request, when it should be POST request?

The request should be a POST with x-www-form-urlencoded data as the POST body.

POST https://login.eveonline.com/oauth/token HTTP/1.1

Content-Type: application/x-www-form-urlencoded
Host: login.eveonline.com
Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l  

grant_type=refresh_token&refresh_token=gEy...fM0

@Qia_Kare esi.evetech.net is the base URL for ESI. When dealing with the SSO flow, login.eveonline.com is the correct host since he is trying to refresh a token.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.