Search code examples
c#model-view-controllersandboxquickbooks-online

Quickbooks Online sandbox returns Waiting for Activation, i have realmId, accesstoken aswell


My Code is as follow:- i have no idea why i am receiving this message, please help. Right now i am using sandbox account to test this. I have generated the data i.e. sample data from API explorer and i am passing it as a parameter as Json.

     public bool GeneratePayment(string JsonData)
        {
            var principal = User as ClaimsPrincipal;
            Session["realmId"] = "XXXXXX";
            if (Session["realmId"] != null)
            {
                string realmId = Session["realmId"].ToString();

                string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];

                //add qbobase url and query
                string uri = string.Format("{0}/v3/company/{1}/invoice", qboBaseUrl, realmId);

                try
                {
                    var client = new HttpClient();


                    client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
                    client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
                    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "XXXX");


//Here i am getting waiting for activation
                    var result = client.PostAsync(uri, new StringContent(JsonData, System.Text.Encoding.UTF8, "application/json"));

                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
            }
            else
                return false;
        }

Solution

  • Has to do with the Task associated with PostAsync.

    The GeneratePayment method needs to be made async and client.PostAsync needs to be awaited as well

    public async Task<bool> GeneratePayment(string JsonData) {
        var principal = User as ClaimsPrincipal;
        Session["realmId"] = "XXXXXX";
        if (Session["realmId"] != null) {
            string realmId = Session["realmId"].ToString();
            string qboBaseUrl = ConfigurationManager.AppSettings["QBOBaseUrl"];
            //add qbobase url and query
            string uri = string.Format("{0}/v3/company/{1}/invoice", qboBaseUrl, realmId);
    
            try {
                var client = http.Value; //singleton http client
                var result = await client.PostAsync(uri, new StringContent(JsonData, System.Text.Encoding.UTF8, "application/json"));
                return true;
            } catch (Exception ex) {
                return false;
            }
        }
        else
            return false;
    }    
    
    //Singleton lazy loaded HttpClieny
    static Lazy<HttpClient> http = new Lazy<HttpClient>(() => {
        var client = new HttpClient();
        client.DefaultRequestHeaders.Add("Accept", "application/json;charset=UTF-8");
        client.DefaultRequestHeaders.Add("ContentType", "application/json;charset=UTF-8");
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + "XXXX");
        return client;
    });