Reputation: 8004
I tired different resources but could not find an answer to my question, if this question has been answered somewhere else then please send me the link for the answer.
I have restful service to consume, to do that I have to use Authentication first. well, that is working fine, I managed the authentication and I get the authentication token.
now when I want to use the service that I want I get The remote server returned an error: (401) Unauthorized.
There is no place in the service to use the token.
I created a simple C# program to do that it contains 2 buttons button 1 : will Authenticate user (works fine and I get the token) button 2 : will use the main service (does not work and get Unauthorized)
here is my code please advice how should I use the authentication token.
private void button1_Click(object sender, EventArgs e)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://apps.ramm.co.nz:443/RammApi6.1/v1/authenticate/login");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{\"database\":\"RAMM API Demo\"," +
" \"userName\":\"api_demo\"," +
"\"password\":\"thursday\"}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
.............
private void button2_Click(object sender, EventArgs e)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://apps.ramm.co.nz:443/RammApi6.1/v1/data/table");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "{ \"tableName\": \"carr_way\" " +
", \"expandLookups\": \"False\" " +
", \"getGeometry\": \"True\" " +
", \"loadType\": \"Specified\" " +
", \"columns\": [\"carr_way_no\", \"road_id\", \"carrway_start_m\", \"carrway_end_m\", \"start_name\", \"end_name\", \"added_on\", \"chgd_on\"] " +
", \"filters\": [[{\"columnName\": \"added_on\", \"operator\": \"GreaterThan\", \"value\": \"2015-01-01\"}] " +
", [{\"columnName\": \"chgd_on\", \"operator\": \"GreaterThan\", \"value\": \"2015-01-01\"}]]}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
Upvotes: 0
Views: 2174
Reputation: 651
You would typically put the authorization in the authorization header, but depending on what type of authorization you are using, that may depend. This may be of help to you:
Setting Authorization Header of HttpClient
Upvotes: 1