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

**URL:** <https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264>\
**Category:** Third Party Developers\
**Created:** [April 22, 2019, 3:31am UTC](https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264 "2019-04-22T03:31:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ParaPants](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/parapants/32/112232_2.png) [@ParaPants](https://forums.eveonline.com/u/ParaPants)\
**Post date:** [April 22, 2019, 3:31am UTC](https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264/1 "2019-04-22T03:31:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Qia\_Kare](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/qia_kare/32/279281_2.png) [@Qia\_Kare](https://forums.eveonline.com/u/Qia_Kare)\
**Post date:** [April 22, 2019, 5:10am UTC](https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264/2 "2019-04-22T05:10:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Golden\_Gnu](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/golden_gnu/32/459734_2.png) [@Golden\_Gnu](https://forums.eveonline.com/u/Golden_Gnu)\
**Post date:** [April 22, 2019, 10:11am UTC](https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264/3 "2019-04-22T10:11:01Z")

</div>

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:

> **[devfleet/awesome-eve](https://github.com/devfleet/awesome-eve#net)**
>
> A list of 3rd party Applications and Tools for the awesome Eve Online MMO. - devfleet/awesome-eve

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

> **[esi/esi-docs](https://github.com/esi/esi-docs/)**
>
> The official repository for ESI documentation. Contribute to esi/esi-docs development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![Captain\_Panther](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/captain_panther/32/368735_2.png) [@Captain\_Panther](https://forums.eveonline.com/u/Captain_Panther)\
**Post date:** [April 22, 2019, 10:54am UTC](https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264/4 "2019-04-22T10:54:48Z")

</div>

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

Line 215 RefreshToken() method

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://sea1.discourse-cdn.com/eveonline/user_avatar/forums.eveonline.com/blacksmoke16/32/376589_2.png) [@Blacksmoke16](https://forums.eveonline.com/u/Blacksmoke16)\
**Post date:** [April 22, 2019, 12:21pm UTC](https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264/5 "2019-04-22T12:21:43Z")

</div>

@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.

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/eveonline/original/3X/4/1/41ec4921cbaca7a6f9ea556e7cc20018461eae5e.png) [@system](https://forums.eveonline.com/u/system)\
**Post date:** [July 21, 2019, 12:21pm UTC](https://forums.eveonline.com/t/cant-get-it-to-work-c-console-app-refreshing-esi-token/154264/6 "2019-07-21T12:21:45Z")

</div>

Closed on July 21, 2019, 12:21pm UTC

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