question

CzarR-3851 avatar image
0 Votes"
CzarR-3851 asked CzarR-3851 edited

Api call through VS studio Console in c# fails

HI, the Api call (Patch) through Visual Studio Console app is failing with the following error "The remote server returned an error: (403) Forbidden.". The same call works fine when I use Postman(Basic authentication with username and password). I get the response back from Postman. What could be the issue with the c# program. Please suggest. Thanks in advance. .NET framework 4.7.

 using System;
 using System.Data;
 using System.Data.SqlClient;
 using System.IO;
 using System.Linq;
 using System.Net;
 using System.Globalization;
 using System.Text.RegularExpressions;
 using System.Xml;
 using System.Web;
    
 namespace ConsoleApp1
 {
     class Program
     {
         static void Main(string[] args)
         {
             var result = string.Empty;
             string json = @"{
 ""schemas"": [
         ""urn:scim:schemas:core:2.0:User"",
         ""urn:scim:schemas:extension:fa:2.0:faUser""
     ],
     ""userName"": ""ZMatt.Dandon @cmpy.com"",
     ""name"": {
         ""familyName"": ""Dandon"",
         ""givenName"": ""ZMatt""
     },
     ""displayName"": ""ZMatt Dandon"",
     ""preferredLanguage"": ""en"",
     ""active"": false
 }";
             string url = @"https://url/hcmRestApi/scim/Users/C8FF94E381891376E050480A69294891";
             ServicePointManager.Expect100Continue = true;
             ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    
             // Making Web Request
             HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(url);
             Req.Credentials = new NetworkCredential("UserName", "Password");
    
             //Content_type    
             Req.ContentType = "application/vnd.oracle.adf.action+json";
    
             //HTTP method    
             Req.Method = "PATCH";
    
             using (var streamWriter = new StreamWriter(Req.GetRequestStream()))
             {
                 streamWriter.Write(json);
             }
    
             var httpResponse = (HttpWebResponse)Req.GetResponse();
             using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
             {
                 result = streamReader.ReadToEnd();
             }
    
    
         }
     }
 }
dotnet-csharppartner-center-api
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AgaveJoe avatar image
0 Votes"
AgaveJoe answered

Basic authentication is base64 encoded and added to the HTTP Authorization header. See this SO post.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Paul-5034 avatar image
0 Votes"
Paul-5034 answered CzarR-3851 commented

Are "UserName" and "Password" your real credentials?

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

They are not mine. They were given to us. Also, to be sure I put break point in my Visual Studio and copied all the values from Visual studio to the Postman.

0 Votes 0 ·

Could it be that the server is rejecting your request based on the user agent? If it works from the browser but not the app it might be worth checking if it works when you set this:

 Req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36";
1 Vote 1 ·

This was a good idea but it failed with this also.

0 Votes 0 ·
Show more comments
CzarR-3851 avatar image
0 Votes"
CzarR-3851 answered CzarR-3851 edited

OK, @AgaveJoe that is correct. THank you so much for the answer.

Here is the updated code

   // Making Web Request
     HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(url);
     //Req.Credentials = new NetworkCredential("00OracleERPuser", "PMT12345");
     string encoded = System.Convert.ToBase64String(Encoding.GetEncoding("UTF-8")
                        .GetBytes("UserName" + ":" + "Password"));
     Req.Headers.Add("Authorization", "Basic " + encoded);
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.