RDG KnowledgeBase C# example

62 views
Skip to first unread message

WantStuff

unread,
Dec 10, 2017, 1:16:59 PM12/10/17
to A gathering place for the Open Rail Data community
Hi guys,

I'm new to the group so feel free to redirect me.


Can anyone share a C#/.NET example or give .NET guidance on fetching data via a Knowledge base feed?

I have been through the nrodwiki KnowledgeBase and believe I have set my datafeeds profile correctly, but am receiving a 401 error on the authenticate POST call.
My suspicion is that I am not adding my credentials correctly for the POST to authenticate (rather than not having permission),
e.g. it is the "Raw payload: username=us...@gmail.com&password=P@55w0rd1" I am struggling to make work - I am using the same account and password that I use to manually log into datafeeds.nationalrail.co.uk

Thanks,
Martin

WantStuff

unread,
Dec 11, 2017, 2:23:39 AM12/11/17
to A gathering place for the Open Rail Data community
Not to worry, I was being a muppet.
I assumed I had to use 'real' authentication rather than just adding my credentials in the message body.


Here's a code sample in case it helps anyone else (C# 4.6.2)
const string username = "em...@gmail.com";
const string password = "MyPassword";

var client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpContent content = new StringContent($"username={username}&password={password}", Encoding.UTF8, "application/x-www-form-urlencoded");

var uri = new Uri("https://datafeeds.nationalrail.co.uk/authenticate");
var response = client.PostAsync(uri, content).Result;
if (!response.IsSuccessStatusCode)
{
    throw new AggregateException($"{response.StatusCode} ({response.ReasonPhrase})");
}

return response.Content.ReadAsStringAsync().Result;


Reply all
Reply to author
Forward
0 new messages