如何解决:“ q”的参数验证失败

时间:2019-11-07 20:57:12

标签: c# xml google-translate

我需要使用Google翻译API,但对我来说仅翻译100行。实际上我有5000行,我必须给5000行。可能存在配额问题,但我知道100秒内可以达到1000万。

如果您能帮助我,我将非常高兴。谢谢。

翻译100行后,它会给我this error

我的api代码:

public static string TranslateText(string data) //Google translate API 
{
    Thread.Sleep(500);
    //Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"(--api key path--)");
    Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", @"(--api key path--);
    string Pathsave = Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS");
    TranslationClient client = TranslationClient.Create();

    string translate_result = client.TranslateText(data, "en", "de").TranslatedText;
    return translate_result;
}    

导致异常的代码;

public string EditText_Product(XDocument xDocument)
{
        try
        {
            var stbu = new StringBuilder();

            stbu.AppendLine("asdasdasd");

            string a, b, c, d, e, vid;


            SQLiteConnection sql_con = new SQLiteConnection
            ("data source= --database path --");

            if (sql_con.State != ConnectionState.Open)
                sql_con.Open();

            foreach (XElement root in xDocument.Descendants("Root"))
            {

                foreach (XElement products in root.Descendants("PRODUCTS"))
                {

                    foreach (XElement product in products.Descendants("PRODUCT"))
                    {
                        a = (string)product.Element("A");
                        a = StripHTML(a);
                        a = TranslateText(a);

                        b = (string)product.Element("B");
                        b = StripHTML(B);
                        if (b != null)
                        {
                            b = TranslateText(b).Trim();
                        }


                        c = (string)product.Element("C");
                        c = StripHTML(c);
                        if (c != null)
                        {
                            c = TranslateText(C).Trim();
                        }



                        foreach (XElement options in product.Descendants("OPTIONS"))
                        {
                            foreach (XElement option in options.Descendants("OPTION"))
                            {

                                d = (string)option.Element("D");
                                d = StripHTML(d);

                                e = (string)option.Element("E");
                                e = StripHTML(e).Trim();

                                vid = (string)option.Element("VID");


                                if (BarcodeExist_Product(vid) == false)
                                {

                                    stbu.AppendLine(stokkodu + "," + "default_family" + "," + "enabled" + "," + "simple" + "," + "each" + "," + "in_stock" +
                                                    "," + "3" + "," + "1" + "," + "1" + "," + "item" + "," + "1" + "," + "1" + "," + "1" + "," + "" + "," + a +
                                                    "," + "system" + "," + a + "," + "" + "," + b + "," + "system" + "," + b + "," + "" + "," +
                                                    c + "," + "system" + "," + c + "," + "" + "," + "" + "," + "1" + "," + "1" + "," + "" +
                                                    "," + "" + "," + "system" + "," + "" + "," + "" + "," + "" + "," + "system" + "," + "" +
                                                    "," + "" + "," + "" + "," + "system" + "," + "" + "," + "" + "," + "" + "," +
                                                    "system" + "," + "" + "," + "" + "," + "");

                                    WriteToFile_Product(stbu.ToString());

                                    SaveToDB_Product(vid);
                                }                                    
                            }
                        }
                    }
                }
            }

            return null;

        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            sql_con.Close();

        }
    }
}

0 个答案:

没有答案