Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public PXAction<UserReportEx> Test;
- [PXButton(CommitChanges = true)]
- [PXUIField(DisplayName = "Test", Visibility = PXUIVisibility.Visible)]
- protected virtual void test()
- {
- string name = "Username";
- string tenant = "@" + "Tenant";
- string password = "Password";
- string sessionToken = string.Empty;
- var client = new RestClient("http://127.0.0.1:7778")
- {
- Authenticator = new HttpBasicAuthenticator(name + tenant, password)
- };
- foreach (UserReportEx report in Reports.Select())
- {
- var ext = report.GetExtension<UserReportExt>();
- if (ext != null && ext.UsrSelected == true)
- {
- // Step 1: Prepare the HTTP POST request for login
- var loginRequest = new RestRequest("/api/servicegate.asmx/Login", Method.POST);
- loginRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
- // Add form data for login (username and password)
- loginRequest.AddParameter("name", name + tenant);
- loginRequest.AddParameter("password", password);
- // Execute the login request
- IRestResponse loginResponse = client.Execute(loginRequest);
- PXTrace.WriteInformation("Login Response: " + loginResponse.Content + " Status code: " + loginResponse.StatusCode);
- // Check if login was successful
- if (loginResponse.StatusCode == System.Net.HttpStatusCode.OK && loginResponse.Content.Contains("<Code>OK</Code>"))
- {
- // Step 2: Extract session token from XML (using Acumatica namespace)
- XDocument loginXml = XDocument.Parse(loginResponse.Content);
- sessionToken = loginXml.Root.Element("{http://acumatica.com}Session").Value;
- PXTrace.WriteInformation("Login successful! Session Token: " + sessionToken);
- // Extract Set-Cookie header if exists
- var cookies = loginResponse.Headers
- .Where(h => h.Name == "Set-Cookie")
- .Select(h => h.Value.ToString());
- var sessionCookies = string.Join("; ", cookies);
- PXTrace.WriteInformation("Session Cookies: " + sessionCookies);
- // Step 3: Prepare the HTTP POST request for saving the report with session token and cookies
- var reportRequest = new RestRequest("/api/servicegate.asmx/ReportSaveEx", Method.POST);
- // Set the session token in the Authorization header
- reportRequest.AddHeader("Authorization", "Bearer " + sessionToken);
- // Set the session cookies in the Cookie header
- reportRequest.AddHeader("Cookie", sessionCookies);
- // Set the content type for the report save request
- reportRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
- // Add the necessary parameters
- reportRequest.AddParameter("name", report.ReportFileName); // Report name
- reportRequest.AddParameter("xml", report.Xml); // XML content of the report
- reportRequest.AddParameter("createVersion", "true"); // Whether to create a new version
- reportRequest.AddParameter("versiondescription", "Neue Version des Reports"); // Description of the new version
- reportRequest.AddParameter("company", "Firma"); // Company identifier
- reportRequest.AddParameter("screenID", "Screen123"); // Screen ID if needed
- reportRequest.AddParameter("uiSave", "true"); // UI Save flag, if applicable
- // Execute the report save request
- IRestResponse reportResponse = client.Execute(reportRequest);
- PXTrace.WriteInformation("Report Response: " + reportResponse.Content + " Status code: " + reportResponse.StatusCode);
- // Log the response headers
- foreach (var header in reportResponse.Headers)
- {
- PXTrace.WriteInformation($"Header: {header.Name} - Value: {header.Value}");
- }
- PXTrace.WriteInformation("Response Content: " + reportResponse.Content);
- // Check for authentication error
- if (reportResponse.Content.Contains("Not authenticated"))
- {
- PXTrace.WriteError("Error: Not authenticated. Please check session token and cookies.");
- }
- }
- else
- {
- PXTrace.WriteError("Login failed. Status: " + loginResponse.StatusCode + " Response: " + loginResponse.Content);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment