none
C# Oracle Rest API, Authentication Issue RRS feed

  • Question

  • I am trying to use Ocacle's Financial REST API and I'm having trouble making it work in C# in VS2019.

    I can confirm the restful call works using Postman, so I know my credentials are fine but I must be missing something trying this with in code.

    So URL is like so: http://MYCLOUDDOMAIN/fscmRestApi/resources/11.13.18.05/ledgerBalances?finder=AccountBalanceFinder;accountCombination=3312-155100-0000-0000-0000-00000,accountingPeriod=Feb-20,currency=USD,ledgerSetName=Ledger,mode=Detail&fields=LedgerName,PeriodName,Currency,DetailAccountCombination,Scenario,BeginningBalance,PeriodActivity,EndingBalance,AmountType,CurrencyType,ErrorDetail

    So I stick that in postman, put in my credentials (basic auth) and it works find. In VS I've tried both the RestSharp way and basic HTTPRequest way as follows:

    HttpWebRequest r = (HttpWebRequest)WebRequest.Create("/fscmRestApi/resources/11.13.18.05/ledgerBalances?finder=AccountBalanceFinder;accountCombination=3312-155100-0000-0000-0000-00000,accountingPeriod=Feb-20,currency=USD,ledgerSetName=Ledger US,mode=Detail&fields=LedgerName,PeriodName,Currency,DetailAccountCombination,Scenario,BeginningBalance,PeriodActivity,EndingBalance,AmountType,CurrencyType,ErrorDetail");
            r.Method = "GET";
    
            string auth = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("Username" + ":" + "Password"));
            r.Headers.Add("Authorization", "Basic" + " " + auth);
            r.ContentType = "application/vnd.oracle.adf.resourcecollection+json";
    
            using (HttpWebResponse resp = (HttpWebResponse)r.GetResponse())
            {
                int b = 0;
            }

    RestSharp:

                var client = new RestClient("http://MYCLOUDDOMAIN/fscmRestApi/resources/11.13.18.05/ledgerBalances?finder=AccountBalanceFinder;accountCombination=3312-155100-0000-0000-0000-00000,accountingPeriod=Feb-20,currency=USD,ledgerSetName=Ledger US,mode=Detail&fields=LedgerName,PeriodName,Currency,DetailAccountCombination,Scenario,BeginningBalance,PeriodActivity,EndingBalance,AmountType,CurrencyType,ErrorDetail");
            client.Authenticator = new RestSharp.Authenticators.HttpBasicAuthenticator("UserName", "Password");
    
            //Tried authorization this way as well.
            //JObject AuthRequest = new JObject();
            //AuthRequest.Add("Username", "UserName");
            //AuthRequest.Add("Password", "Password");
    
            var request = new RestRequest();
            request.Method = Method.GET;
            request.RequestFormat = DataFormat.Json;
            //request.AddParameter("text/json", AuthRequest.ToString(), ParameterType.RequestBody);
            request.AddHeader("Content-Type", "application/vnd.oracle.adf.resourcecollection+json");
            request.AddHeader("REST-Framework-Version", "1");
            var response = client.Get(request);

    No matter what I try I am always 401 not authorized. I suspect its some kind of header thing? I can't see the raw request header in postman

    I am new to REST. I am used to using WSDLs soap services.

    Friday, May 22, 2020 1:38 PM

All replies

  • Maybe, you can be helped at the forumn, since a RestApi is a Web service.

    https://forums.asp.net/28.aspx/1?WCF+ASMX+and+other+Web+Services

    Saturday, May 23, 2020 1:16 PM
  • Questions related to calling a particular providers API should be posted in their forums. They are going to be better able to help you with the required format. In your case you said you are able to get it to work in Postman so it should work in C# the same way. I suspect you're just not setting up your C# code correctly but you are not using the core library for this. You're using RestSharp which is a third party product. Questions related to using them should be posted in their forums. My gut instinct is that the issue is specifically with how you're using the client but since we don't support third party products here I really cannot help on that matter.

    I recommend that you take the working query in Postman and then use the Code button in the top right corner of the request window and select C#. It should auto generate the RestClient code that you need to replicate that request. You should be able to run it in your app and get the same results.

    One the issues I see with your code is that you're likely setting the content type more than once. You appear to be configuring the request format for JSON and then adding a content-type that is different. I have no knowledge of the implementation of that code in RestSharp but they might be conflicting. At a minimum it would send both content-type formats (because headers are lists of values) which might cause problems. I should also point out that you don't need any of this because GET requests don't have a body and therefore `Content-Type` is meaningless on a GET request. However you generally have to specify the content types you accept using the Accept header. This should probably be set to JSON.

    However you mentioned it is a 401 which is an auth issue. Again we don't support third party libraries so I have no idea what Authenticator does but I assume it is a simple wrapper around the `Authorization` header which appears to be using Basic. All this will have been configured in Postman so you should be able to look there to see what the authentication model and values should be.


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, May 23, 2020 2:53 PM