cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Authentication issues when using RESTful api C# for 4.2 BusinessObjects

uhs
Explorer
0 Kudos
1,093

Hi there,

I am trying to connect to the RESTful api using c#, but having some difficulties getting the syntax correct.

I'm currently receiving a 401 response error. I am an administrator and the RESTful base url is setup.

I have the required response body:

<attrs xmlns="http://www.sap.com/rws/bip">
<attr name="password" type="string"/>
<attr name="clientType" type="string"/>
<attr name="auth" type="string" possibilities="secEnterprise,secLDAP,secWinAD,secSAPR3">secEnterprise</attr>
<attr name="userName" type="string"/>
</attrs>

The C# code I'm using is:

        public async Task<string> AuthenticateAsync()
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                // Construct the XML request body
                var requestBody = $@"
                    <attrs xmlns=""http://www.sap.com/rws/bip""> 
                        <attr name=""password"" type=""string"">{password}</attr>
                        <attr name=""clientType"" type=""string"">SDK</attr>
                        <attr name=""auth"" type=""string"" possibilities=""secEnterprise,secLDAP,secWinAD,secSAPR3"">secWinAD</attr>
                        <attr name=""userName"" type=""string"">{username}</attr>
                    </attrs>";

                StringContent content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/xml");
             
                var response = await client.PostAsync($"{baseUrl}/logon/long", content);
                GD.Print("response", response);

                if (response.IsSuccessStatusCode)
                {
                    var responseData = await response.Content.ReadAsStringAsync();
                    var xmlDoc = new System.Xml.XmlDocument();
                    xmlDoc.LoadXml(responseData);

                    // Assuming the token is in a specific XML element
                    var tokenNode = xmlDoc.SelectSingleNode("//attr[@name='LogonToken']");
                    if (tokenNode != null)
                {
                        string logonToken = tokenNode.InnerText.Trim();
                        return logonToken;
                }
                    else
                    {
                        throw new Exception("Token not found in the response.");
                    }
                }
                else
                {
                    throw new Exception("Authentication failed: " + response.ReasonPhrase);
                }
            }
        }

At this stage I comment out what's in the if statement as I am only interested in the response.

I have tried it without the clientType as I am not entirely sure what that needs to be.

I was hoping someone could see something I've missed.

Thanks,

Paul

 

 

 

Accepted Solutions (1)

Accepted Solutions (1)

ayman_salem
Active Contributor
0 Kudos

what base URL are you using?  it should be  http(s)://<server>:<RESTful port>/biprws

You also missed the header   "Accept"  application/xml

 

uhs
Explorer
0 Kudos

Hi ayman_salem,
Thanks for reply.

The baseurl is in the form of http://<server>:6405/biprws.

I had added this syntax previously but didn't see a change:

using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
{
	// Add the Accept header to the request
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xml"));
// code continued here...

 This is the reponses I get both when hardcoding the credentials and baseurl and with variables :

responseStatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Server: Apache-Coyote/1.1
  Access-Control-Allow-Credentials: true
  Access-Control-Allow-Origin: http://<servername>:8080
  Access-Control-Expose-Headers: X-SAP-LogonToken, X-SAP-PVL, WWW-Authenticate
  Date: Tue, 03 Sep 2024 05:22:23 GMT
  Content-Type: application/xml
  Content-Length: 431
}

or

responseStatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.HttpConnectionResponseContent, Headers:
{
  Server: Apache-Coyote/1.1
  Date: Tue, 03 Sep 2024 05:30:13 GMT
  Content-Type: application/xml
  Content-Length: 431
}

The url to get the response works so assume the api is accepting requests

http://servername:6405/biprws/logon/long

Is there a log file I can check for more debug information. I wonder if the requestBody syntax is not correct.

Answers (0)