working with whm api using c#.net
We tried to create an api interface using c#.net winforms but failed to login due 403 error
code is attached for debugging
string url = "https://servername:2087/cpsess8074801816/xml-api/version";
WebRequest wreq = WebRequest.Create(url);
CredentialCache MyCredential = new CredentialCache();
MyCredential.Add(new Uri("https://servername:2087/cpsess8074801816/xml-api/version"), "Basic", new NetworkCredential("root", "password"));
wreq.Credentials = MyCredential;
wreq.Timeout = 8000;
WebResponse wres = wreq.GetResponse();
if (((HttpWebResponse)wres).StatusDescription == "OK")
{
wres.Close();
// //Code if OK
}
else
{
wres.Close();
// //Code if not OK
}
-
Hello :) Could you let us know the specific output to /usr/local/cpanel/logs/login_log when authentication fails? Also, you may find this document helpful: Guide to API Authentication - Software Development Kit - cPanel Documentation Thank you. 0 -
Not Log Entry was Found.. App Gave Error as : The remote server returned an error: (403) Forbidden. 0 -
Entry was not found in log at /usr/local/cpanel/logs/login_log webrequest returned 403 error 0 -
Here are a couple of threads you may find helpful: Get number of allowed addon domains via cpanel API cPanel login rejected with API through C# Thank you. 0 -
issue resolved by creating additional layer to get session tokens thanks for all your support 0 -
I am happy to see the issue is now resolved. Thank you for updating us with the outcome. 0 -
issue resolved by creating additional layer to get session tokens thanks for all your support
Hey man, Can u share the whole code for the request. I am trying to make the call but my connection is getting closed :( Thanks0 -
Hello @zemimk, Can you confirm if any of the code examples on GitHub at the link below are helpful? GitHub Search Results for the term "cpanel" with the Language filter set to C# Thank you. 0 -
Hello @zemimk, Can you confirm if any of the code examples on GitHub at the link below are helpful? GitHub Search Results for the term "cpanel" with the Language filter set to C# Thank you.
Hi, yes i did check everything but sadly there is no example for WHM as well as they are using Basic Authentication and i want to use WHM token auth. I did try many ways (everything possible that i was able to find on google) but iam keep getting the same error: IOException: The handshake failed due to an unexpected packet format. Here is two variates of codes that i had the highest hopes butthey are still not working and would love to get some help from u for solving this. 1. [CODE=csharp]tring url = "https://my_host_name:2087/json-api/applist?api.version=1"; Uri myUri = new Uri(url, UriKind.Absolute); // string data = this.easwKey + ";" + this.phishKey; byte[] buffer = Encoding.ASCII.GetBytes(""); //the data you want to send to the web service HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(myUri); WebReq.ServerCertificateValidationCallback += delegate { return false; }; WebReq.ContentType = "application/json"; WebReq.Method = "POST"; WebReq.Accept = "*/*"; WebReq.KeepAlive = true; WebReq.Headers["Authorization"> = "whm root:TOKENCODEGENERATEDINWHM"; WebReq.Host = "my_host_name:2087"; WebReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; Stream PostData = WebReq.GetRequestStream(); PostData.Write(buffer, 0, buffer.Length); PostData.Close(); HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse(); Stream Answer = WebResp.GetResponseStream(); StreamReader _Answer = new StreamReader(Answer); string returnStr = _Answer.ReadToEnd();
2. [CODE=csharp]string url = "https://my_host_name:2087/json-api/applist?api.version=1"; WebRequest wreq = WebRequest.Create(url); HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest; ServicePointManager.ServerCertificateValidationCallback += delegate { return true; }; string auth = "whm " + Convert.ToBase64String(System.Text.Encoding.Default.GetBytes("root:MYTOKENFROMWHM")); req.PreAuthenticate = true; req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; req.Headers.Add("Authorization", auth); req.UserAgent = "cPanel .NET C# authenticator script"; WebResponse resp = req.GetResponse(); Stream receiveStream = resp.GetResponseStream(); StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8); string content = reader.ReadToEnd();0 -
Hello @zemimk, Consider uploading an example of what you have thus far to a new GitHub repo. Then, share a link to the repo here. This is more likely to inspire others to share suggestions or submit changes. The #integrations channel on our Discord chat is another resource you may want to explore: Join the cPanel Discord Server! Thanks! 0 -
Hello @zemimk, Consider uploading an example of what you have thus far to a new GitHub repo. Then, share a link to the repo here. This is more likely to inspire others to share suggestions or submit changes. The #integrations channel on our Discord chat is another resource you may want to explore: Join the cPanel Discord Server! Thanks!
So i wont get any suggestions how to fix my problem here? I see there are people that are already sharing code here. Would love to share working code on git but so far it is useless. After i turn off SSl validation i stop getting the error metion above but in the response i am getting html code from the login page of WHM instead of json string of the list of the accounts.0 -
Hello @zemimk, Consider uploading an example of what you have thus far to a new GitHub repo. Then, share a link to the repo here. This is more likely to inspire others to share suggestions or submit changes. The #integrations channel on our Discord chat is another resource you may want to explore: Join the cPanel Discord Server! Thanks!
can u help me about my second question? Why instad of getting json response i am getting HTML code from WHM login page?0 -
Hello @zemimk, It looks like you are making some progress with the help of the #integrations channel on the cPanel Discord server. Feel free to post the outcome here in the future once you get everything working as intended. Thanks! 0
Please sign in to leave a comment.
Comments
13 comments