Forum Discussion

ug24's avatar
6 months ago

Encountered a communication error (https://api.myob.com/accountright)

Hi, 
I am trying to fetch the accounts using my .NET application, built over the same version as MYOB SDK.
When I hit the API I get "a communication error (https://api.myob.com/accountright)" like this

How can I resolve this error and what should I do for that?
 
Code: 

public ActionResult GetContacts()
        {
            try
            {
                var accessToken = HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
                var expiresIn = HttpContext.Request.Headers["ExpiresIn"];
                var hasExpired = HttpContext.Request.Headers["HasExpired"];
                var receivedTime = HttpContext.Request.Headers["ReceivedTime"];
                var refreshToken = HttpContext.Request.Headers["RefreshToken"];
                var scope = HttpContext.Request.Headers["Scope"];
                var tokenType = HttpContext.Request.Headers["TokenType"];

                var tokenValues = new TokenValues
                {
                    AccessToken = accessToken,
                    ExpiresIn = int.Parse(expiresIn),
                    HasExpired = bool.Parse(hasExpired),
                    ReceivedTime = DateTime.Parse(receivedTime),
                    RefreshToken = refreshToken,
                    Scope = scope,
                    TokenType = tokenType
                };

                var tokenValuesJson = JsonConvert.SerializeObject(tokenValues);
                var keystore = JsonConvert.DeserializeObject<SimpleOAuthKeyService>(tokenValuesJson);

                // Fetch a list of company files
                var cfService = new CompanyFileService(_configuration, null, keystore);
                var companyFiles = cfService.GetRange();
                // Select a company file
                var companyFile = companyFiles.FirstOrDefault(x => new Version(x.ProductVersion) >= new Version("2013.3"));
                if (companyFile == null)
                {
                    return HttpNotFound("No company file found that supports version 2 of the AccountRight API.");
                }

                // Fetch accounts for the selected company file
                var credentials = new CompanyFileCredentials("Administrator", "");
                var accountService = new MYOB.AccountRight.SDK.Services.GeneralLedger.AccountService(_configuration, null, keystore);
                var accounts = accountService.GetRange(companyFile, null, credentials);
                return Ok(accounts);

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error in GetContacts: " + ex.Message);
                Console.WriteLine("Stack trace: " + ex.StackTrace);
                return new HttpStatusCodeResult(500, "Internal Server Error: " + ex.Message);
            }
        }

  • Hi ug24

     

    I understand you're encountering a communication error when trying to fetch accounts using your.NET application built over the same version as MYOB SDK. This error can be caused by various factors, and I'll provide some general troubleshooting steps that might help. Firstly, ensure that your internet connection is stable and reliable. Network issues can often cause communication errors. Check for restrictions or firewalls that could be causing the error. If the problem persists, I recommend reaching out to our support via this link.

     

    Feel free to get in touch if you need more help.

     

    Cheers,

    Princess