0

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();
      }

   }
asmgx
  • 7,328
  • 15
  • 82
  • 143
  • You don't seem to save your authentication token when clicking button 1 and hence you never send your authentication token when clicking button 2... – Sani Huttunen Jun 14 '17 at 01:53
  • @SaniSinghHuttunen How to use it? – asmgx Jun 14 '17 at 01:54
  • Too broad and too unspecified... – Sani Huttunen Jun 14 '17 at 01:54
  • what do you mean Too broad and too unspecified, I am asking how to use a token generated from one service in the other, I have provided the code that I have done.. what is that unspecified.. !!!! – asmgx Jun 14 '17 at 01:56
  • Well... What is the authentication service in the other end? There are TONS of different services and you want me to guess which one you are using? – Sani Huttunen Jun 14 '17 at 01:57
  • the code above shows all the details. you can see the link the userid and the passwords too , I am asking the question because I dont know how to use the authentication.. next time i will find the answer first then ask the question – asmgx Jun 14 '17 at 01:59
  • You do that... Have a good day! – Sani Huttunen Jun 14 '17 at 01:59

1 Answers1

1

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

mp-mi
  • 641
  • 6
  • 6
  • That's just part of OPs problems... Since the backend is Ramm API the authorization part would be an authorization header as you stated. (Authorization: Bearer [token]). However. Since OP doesn't save the authorization toke there is no way of setting it in the next request. And also the URL is wrong in the second request. All this because OP didn't want to read the documentation for the API. – Sani Huttunen Jun 14 '17 at 02:11