Having trouble with getting tokens using v2 oauth

Hello everyone,

I’ve been trying to develop a program using C# and so far I have managed to get to point being able to get the authorization code. However I have become stuck at this part

  1. Now that your application has the authorization code, it needs to send a POST request to https://login.eveonline.com/v2/oauth/token with a payload containing the returned authorization code, the client ID of your application, and the original URL safe Base 64 encoded 32 byte string that was randomly created for the code challenge in step 3. Note: you can look at a Python example of this returning a code verifier here .

My code for this part currently looks like this

   private async Task<Token> GetToken(string clientId, string authorizationCode)
   {
     var httpClient = new HttpClient();
     httpClient.DefaultRequestHeaders.Clear();
     httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Host", "login.eveonline.com");
     httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded");

     var postParam = new Dictionary<string, string>
       {
            {"grant_type", "authorization_code"},
            {"code", authorizationCode},
            {"client_id", clientId},
            {"code_verifier", _originalEncodedString}
        };

        try
        {
            var response = await httpClient.PostAsync(_baseTokenUri, new FormUrlEncodedContent(postParam));

            return JsonConvert.DeserializeObject<Token>(await response.Content.ReadAsStringAsync());
        }
        catch (Exception e)
        {
            Debug.Print(e.Message);
            throw;
        }
    }

where the _originalEncodedString is obtained from the first encoding of the random 32 byte string (before hashing it out and encoding again) using the ff. code

    private byte[] GenerateRandomString(int length = 32)
    {
        var random = new Random();

        byte[] cc = new byte[length];

        random.NextBytes(cc);

        return cc;
    }

    private string EncodeString(byte[] data)
    {
        return Convert.ToBase64String(data).Replace('+', '-').Replace('/', '_').Replace('=', '.');
    }

    private byte[] HashString(string unhashedString)
    {
        byte[] hash;

        using (var sha = new SHA256Managed())
        {
            hash = sha.ComputeHash(Encoding.ASCII.GetBytes(unhashedString));
        }
    
        return hash;
    }

However, whenever I try to test it; it always cause an exception “Internal Server Error”. I have similar code before for the v1 oauth that was working.

Thank you,

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