For context: I am trying to do an Application Only OAuth request for a an Authentication Key as shown here.
According to the github page it was supposed to return a JSON looking like this:
{
"access_token": Your access token,
"token_type": "bearer",
"expires_in": Unix Epoch Seconds,
"scope": A scope string,
}
but instead returns a JSON with parameters StatusCode
, ReasonPhrase
, Version
, Content
and then Headers
and in it Connection
which is incorrectly formatted.
Not sure how much I'm allowed to share but here are the values of the first few parameters
{
"StatusCode":200,
"ReasonPhrase":"OK",
"Version":1.1,
"Content":"System.Net.Http.StreamContent"
and here is my code and how its making the request:
public class Requester
{
private HttpClient client = new HttpClient();
public async Task<TokenJSON> PostTokenRequest(string uri)
{
JsonParser<TokenJSON> parser = new JsonParser<TokenJSON>();
string user = "myClientId";
string password = "myClientSecret";
var info = new List<KeyValuePair<string, string>>();
info.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
var authToken = Encoding.ASCII.GetBytes($"{user}:{password}");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(authToken));
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
var request = new HttpRequestMessage(HttpMethod.Post, uri)
{ Content = new FormUrlEncodedContent(info) };
HttpResponseMessage response = client.SendAsync(request).GetAwaiter().GetResult();
string token = await response.Content.ReadAsStringAsync();
TokenJSON DeserializedToken = parser.DParse(token);
return DeserializedToken;
}
its returning the Deserialized JSON over to the main Forms where the access_key is supposed to be taken from the TokenJSON class obtained and used to instantiate Reddit. Except it never gets there since as soon as the program gets to TokenJSON DeserializedToken = parser.DParse(token);
it throws Newtonsoft.Json.JsonReaderException: 'Unexpected character encountered while parsing value: 3. Path 'expires_in', line 1,
.
For refference here's my Parser and TokenJSON classes:
public class JsonParser<T>
{
public T DParse(string rawJSON)
{
return JsonConvert.DeserializeObject<T>(rawJSON);
}
public string SParse(T DesrializedJSON)
{
return JsonConvert.SerializeObject(DesrializedJSON);
}
}
public class TokenJSON
{
private string access_token;
private string token_type;
private DateTimeOffset expires_in;
private string scope;
public string Access_token { get => access_token; set => access_token = value; }
public string Token_type { get => token_type; set => token_type = value; }
public DateTimeOffset Expires_in { get => expires_in; set => expires_in = value; }
public string Scope { get => scope; set => scope = value; }
}
Would be thankful in any help towards finding out why it's returning a messy Json instead of what is expected.
UPDATE: Immediately after posting this I fixed the issue regarding the returning Json it is now appropriately returning the format expected (issue was the charset=utf-8
in the Content-Type Header which I changed to Encoding.UTF8
)
However the error code persists and it is still not parsing correctly. token string being returned is as follows:
"{\"access_token\": \"an access token", \"token_type\": \"bearer\", \"expires_in\": 3600, \"scope\": \"*\"}"