Guest User

Untitled

a guest
Oct 2nd, 2024
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.81 KB | None | 0 0
  1. public PXAction<UserReportEx> Test;
  2. [PXButton(CommitChanges = true)]
  3. [PXUIField(DisplayName = "Test", Visibility = PXUIVisibility.Visible)]
  4. protected virtual void test()
  5. {
  6. string name = "Username";
  7. string tenant = "@" + "Tenant";
  8. string password = "Password";
  9. string sessionToken = string.Empty;
  10.  
  11. var client = new RestClient("http://127.0.0.1:7778")
  12. {
  13. Authenticator = new HttpBasicAuthenticator(name + tenant, password)
  14. };
  15.  
  16. foreach (UserReportEx report in Reports.Select())
  17. {
  18. var ext = report.GetExtension<UserReportExt>();
  19. if (ext != null && ext.UsrSelected == true)
  20. {
  21. // Step 1: Prepare the HTTP POST request for login
  22. var loginRequest = new RestRequest("/api/servicegate.asmx/Login", Method.POST);
  23. loginRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
  24.  
  25. // Add form data for login (username and password)
  26. loginRequest.AddParameter("name", name + tenant);
  27. loginRequest.AddParameter("password", password);
  28.  
  29. // Execute the login request
  30. IRestResponse loginResponse = client.Execute(loginRequest);
  31. PXTrace.WriteInformation("Login Response: " + loginResponse.Content + " Status code: " + loginResponse.StatusCode);
  32.  
  33. // Check if login was successful
  34. if (loginResponse.StatusCode == System.Net.HttpStatusCode.OK && loginResponse.Content.Contains("<Code>OK</Code>"))
  35. {
  36. // Step 2: Extract session token from XML (using Acumatica namespace)
  37. XDocument loginXml = XDocument.Parse(loginResponse.Content);
  38. sessionToken = loginXml.Root.Element("{http://acumatica.com}Session").Value;
  39. PXTrace.WriteInformation("Login successful! Session Token: " + sessionToken);
  40.  
  41. // Extract Set-Cookie header if exists
  42. var cookies = loginResponse.Headers
  43. .Where(h => h.Name == "Set-Cookie")
  44. .Select(h => h.Value.ToString());
  45.  
  46. var sessionCookies = string.Join("; ", cookies);
  47. PXTrace.WriteInformation("Session Cookies: " + sessionCookies);
  48.  
  49. // Step 3: Prepare the HTTP POST request for saving the report with session token and cookies
  50. var reportRequest = new RestRequest("/api/servicegate.asmx/ReportSaveEx", Method.POST);
  51.  
  52. // Set the session token in the Authorization header
  53. reportRequest.AddHeader("Authorization", "Bearer " + sessionToken);
  54.  
  55. // Set the session cookies in the Cookie header
  56. reportRequest.AddHeader("Cookie", sessionCookies);
  57.  
  58. // Set the content type for the report save request
  59. reportRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
  60.  
  61. // Add the necessary parameters
  62. reportRequest.AddParameter("name", report.ReportFileName); // Report name
  63. reportRequest.AddParameter("xml", report.Xml); // XML content of the report
  64. reportRequest.AddParameter("createVersion", "true"); // Whether to create a new version
  65. reportRequest.AddParameter("versiondescription", "Neue Version des Reports"); // Description of the new version
  66. reportRequest.AddParameter("company", "Firma"); // Company identifier
  67. reportRequest.AddParameter("screenID", "Screen123"); // Screen ID if needed
  68. reportRequest.AddParameter("uiSave", "true"); // UI Save flag, if applicable
  69.  
  70. // Execute the report save request
  71. IRestResponse reportResponse = client.Execute(reportRequest);
  72. PXTrace.WriteInformation("Report Response: " + reportResponse.Content + " Status code: " + reportResponse.StatusCode);
  73.  
  74. // Log the response headers
  75. foreach (var header in reportResponse.Headers)
  76. {
  77. PXTrace.WriteInformation($"Header: {header.Name} - Value: {header.Value}");
  78. }
  79.  
  80. PXTrace.WriteInformation("Response Content: " + reportResponse.Content);
  81.  
  82. // Check for authentication error
  83. if (reportResponse.Content.Contains("Not authenticated"))
  84. {
  85. PXTrace.WriteError("Error: Not authenticated. Please check session token and cookies.");
  86. }
  87. }
  88. else
  89. {
  90. PXTrace.WriteError("Login failed. Status: " + loginResponse.StatusCode + " Response: " + loginResponse.Content);
  91. }
  92. }
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment