myob-oss / AccountRight_Live_API_.Net_SDK

This is the repo for the MYOB AccountRight Live .Net SDK
MIT License
23 stars 43 forks source link

CORS Error #268

Open utkarsh3020 opened 4 months ago

utkarsh3020 commented 4 months ago

When I hit the API from my frontend react app then I get a CORS error

Frontend Code - const fetchContacts = async () => { const oauthKeyService = keystore; fetch("https://localhost:44326/OAuth/GetContacts", { method: "GET", headers: { 'Content-Type': 'application/json', 'OAuth-Key-Service': JSON.stringify(oauthKeyService) }, }) .then((response) => response.json()) .then((data) => { console.log("Contacts received:", data); setContacts(data); }) .catch((error) => { console.error("Error fetching contacts:", error); }); };

Backend Code - ` public ActionResult CreateContact(Customer customer) { try { var oauthKeyServiceJson = HttpContext.Request.Headers["OAuth-Key-Service"]; var keystore = JsonConvert.DeserializeObject(oauthKeyServiceJson); // Ensure keystore is initialized if (keystore == null) { return new HttpStatusCodeResult(500, "OAuth KeyStore is not initialized."); }

            // Fetch the company file
            var cfService = new CompanyFileService(_configuration, null, keystore);
            var companyFile = cfService.GetRange().FirstOrDefault(x => new Version(x.ProductVersion) >= new Version("2013.3"));

            if (companyFile == null)
            {
                return new HttpStatusCodeResult(500, "No valid company file found.");
            }

            // Ensure credentials are valid
            var credentials = new CompanyFileCredentials("Administrator", "");

            // Create contact service and insert customer
            var contactService = new CustomerService(_configuration, null, keystore);
            contactService.Insert(companyFile, customer, credentials);

            return new HttpStatusCodeResult(201, "Customer created successfully");
        }
        catch (ApiValidationException ex)
        {
            Console.WriteLine("Validation Error in CreateContact: " + ex.Message);
            return new HttpStatusCodeResult(400, ex.Message);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error in CreateContact: " + ex.Message);
            return new HttpStatusCodeResult(500, "An error occurred while creating the customer.");
        }
    }

`

The CORS Error I am getting for both creating contact and fetching contact

utkarsh3020 commented 3 months ago

@sawilde Hi, can u please help me regarding this issue

sawilde commented 3 months ago

Hi @utkarsh3020

I haven't worked on this project in 8 years, ever since I moved on from MYOB.

However, if you are accessing any API from backend code then you shouldn't be troubled by CORS as that is purely a browser thing.

utkarsh3020 commented 3 months ago

Hi @utkarsh3020

I haven't worked on this project in 8 years, ever since I moved on from MYOB.

However, if you are accessing any API from backend code then you shouldn't be troubled by CORS as that is purely a browser thing.

I am accessing the API from the frontend which calls my backend, and then it calls the MYOB accounting server

sawilde commented 3 months ago

Then is the CORS error due to your backend not supporting CORS with your frontend? What backend server are you using and have you enabled CORS on it?