Connect Google Translate URL to 403 Error

35 views
Skip to first unread message

xinfe...@gmail.com

unread,
Jan 20, 2017, 12:34:43 AM1/20/17
to AdWords API Forum
Hello all,

        I registered the account of the Google translation, and the translation of the API key, but in the translation, but reported URL 403 error, why so, how to solve?
        Google translation of URL for http://www.googleapis.com/language/translate/v2? Key=MyAppKey&source=en&target=it  

        My Code is:
         private const string TRANSLATE_API_FORMAT = "http://www.googleapis.com/language/translate/v2?key={0}&source={1}&target={2}";

        private string BuildPostData(params string[] source)
        {
            const string QUERY_PARAM = "q";
            StringBuilder data = new StringBuilder();
            foreach (var item in source)
            {
                data.AppendFormat("{0}={1}&", QUERY_PARAM,  HttpUtility.UrlEncode(item));
            }
            if (data.Length > 0)
            {
                data.Remove(data.Length - 1, 1);
            }
            return data.ToString();
        }

        private GoogleTranslateResult PostDataToTranslate(params string[] source)
        {
            string data = BuildPostData(source);

            Encoding encoding = Encoding.UTF8;
            byte[] bytesToPost = encoding.GetBytes(data);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(TranslateUrl);
            request.Headers.Add("X-HTTP-Method-Override", "GET");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
             
            request.ContentLength = bytesToPost.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(bytesToPost, 0, bytesToPost.Length);
                requestStream.Close();
            }
            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(stream, encoding))
                    {
                        string jsonData = sr.ReadToEnd();
                        GoogleTranslateResult result = JsonConvert.DeserializeObject<GoogleTranslateResult>(jsonData);
                        result.SourceData = source;
                        return result;
                    }
                }
            }
        }



Thank you!
       

Joyce Lava

unread,
Jan 20, 2017, 1:23:00 AM1/20/17
to AdWords API Forum
Hello,

This forum is dedicated for AdWords API-related concerns only. I'm afraid we are not the right channel to help with the above question.

Thanks,
Joyce, AdWords API Team
Reply all
Reply to author
Forward
0 new messages