Error creating ad unit using Host APIs

69 views
Skip to first unread message

Tony John

unread,
Jul 25, 2013, 2:36:36 AM7/25/13
to adsen...@googlegroups.com
I am getting an error when trying to create an ad unit. When I execute the API, I am getting the error "The remote server returned an error: (400) Bad Request.".

Below is the code I am using. Do you see anything wrong with this? (The approach I am using is working fine for some other API calls like List adClients.

        public static void CreateAdUnit(string accountId)
        {
            string access_token = System.Web.HttpContext.Current.Session["Google_Access_Token"] as string;

            StringBuilder postBuilder = new StringBuilder();
            postBuilder.AppendFormat("{0}={1}", "key", Uri.EscapeUriString(apiKey));
            postBuilder.AppendFormat("&{0}={1}", "access_token", Uri.EscapeUriString(access_token));

            string hostUrl = "https://www.googleapis.com/adsensehost/v4.1/accounts/" + accountId + "/adclients/ca-" + accountId + "/adunits";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(hostUrl + "?" + postBuilder.ToString());
            request.Method = "POST";
            request.Accept = "text/json";

            AdSenseAdUnitResource adUnitResource = new AdSenseAdUnitResource();
           
            adUnitResource.contentAdsSettings = new AdSenseAdContentAdsSettings();
           
            adUnitResource.contentAdsSettings.backupOption = new AdSenseAdBackupOption();
            adUnitResource.contentAdsSettings.backupOption.color = "FFFFFF";
            adUnitResource.contentAdsSettings.size = "SIZE_300_250";
            adUnitResource.contentAdsSettings.type = "TEXT";

            adUnitResource.customStyle = new AdSenseAdCustomStyle();
            adUnitResource.customStyle.corners = "VERY_ROUNDED";
           
            adUnitResource.customStyle.font = new AdSenseAdCustomStyleFont();
            adUnitResource.customStyle.font.family = "ACCOUNT_DEFAULT_FAMILY";
            adUnitResource.customStyle.font.size = "ACCOUNT_DEFAULT_SIZE";
           
            adUnitResource.customStyle.colors = new AdSenseAdCustomStyleColors();
            adUnitResource.customStyle.colors.background = "FFFFFF";
            adUnitResource.customStyle.colors.border = "FF0000";
            adUnitResource.customStyle.colors.text = "00FF00";
            adUnitResource.customStyle.colors.title = "000000";
            adUnitResource.customStyle.colors.url = "0000FF";
           
            adUnitResource.name = "Demo Ad1";

            JavaScriptSerializer js = new JavaScriptSerializer();
            string jsonData = js.Serialize(adUnitResource);

            using (var streamWriter = new StreamWriter(request.GetRequestStream()))
            {
                streamWriter.Write(jsonData);
                streamWriter.Flush();
                streamWriter.Close();
            }

            try // Get the response of the POST
            {
                HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();

                StreamReader sr = new StreamReader(webResponse.GetResponseStream());

                string responseData = sr.ReadToEnd().Trim();
            }
            catch (WebException wex)
            {
                // The remote server returned an error: (400) Bad Request.
                throw wex;
            }
            catch
            {
                throw;
            }
        }

Thanks.

Jose Alcérreca (AdSense API Team)

unread,
Jul 25, 2013, 6:34:13 AM7/25/13
to adsen...@googlegroups.com
Hi Tony,

I can't debug your code, but it looks good. Can you copy the actual JSON request? Have you tried using the APIs explorer with the same parameters?

Cheers!
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902

Tony John

unread,
Jul 26, 2013, 6:36:21 AM7/26/13
to adsen...@googlegroups.com
Jose,

I did not have any success with the API Explorer as well, but that gives a different error. Whatever Account Id I use, I am getting an error saying the account id is not valid.

Here is the JSON data I am passing in the request body:


{\"code\":\"\",\"id\":\"\",\"kind\":\"\",\"name\":\"Demo Ad1\",\"customStyle\":{\"kind\":\"\",\"corners\":\"VERY_ROUNDED\",\"colors\":{\"border\":\"FF0000\",\"title\":\"000000\",\"background\":\"FFFFFF\",\"text\":\"00FF00\",\"url\":\"0000FF\"},\"font\":{\"family\":\"ACCOUNT_DEFAULT_FAMILY\",\"size\":\"ACCOUNT_DEFAULT_SIZE\"}},\"contentAdsSettings\":{\"type\":\"TEXT\",\"size\":\"SIZE_300_250\",\"backupOption\":{\"type\":\"COLOR\",\"color\":\"FFFFFF\",\"url\":\"\"}},\"mobileContentAdsSettings\":{\"type\":\"\",\"size\":\"\",\"markupLanguage\":\"\",\"scriptingLanguage\":\"\"}}

Do you see anything wrong in the JSON data?

Tony John

unread,
Jul 26, 2013, 6:41:57 AM7/26/13
to adsen...@googlegroups.com
In the previous response, I had not used the correct value for "kind" property. I have updated it now and here is the new JSON data for request body:

"{\"code\":\"\",\"id\":\"\",\"kind\":\"adsensehost#adUnit\",\"name\":\"Demo Ad1\",\"customStyle\":{\"kind\":\"adsensehost#adStyle\",\"corners\":\"VERY_ROUNDED\",\"colors\":{\"border\":\"FF0000\",\"title\":\"000000\",\"background\":\"FFFFFF\",\"text\":\"00FF00\",\"url\":\"0000FF\"},\"font\":{\"family\":\"ACCOUNT_DEFAULT_FAMILY\",\"size\":\"ACCOUNT_DEFAULT_SIZE\"}},\"contentAdsSettings\":{\"type\":\"TEXT\",\"size\":\"SIZE_300_250\",\"backupOption\":{\"type\":\"COLOR\",\"color\":\"FFFFFF\",\"url\":\"\"}},\"mobileContentAdsSettings\":{\"type\":\"\",\"size\":\"\",\"markupLanguage\":\"\",\"scriptingLanguage\":\"\"}}"




On Thursday, 25 July 2013 16:04:13 UTC+5:30, Jose Alcérreca (AdSense API Team) wrote:

Jose Alcérreca (AdSense API Team)

unread,
Jul 26, 2013, 6:59:26 AM7/26/13
to adsen...@googlegroups.com
Hi Tony,

It could be related to the empty mobileContentAdsSettings but you should use the APIs explorer for this. Authenticate with your host account (sandbox or production). Make sure the account ID is your publisher's and that the ad client ID belongs to the publisher account.

https://developers.google.com/adsense/host/v4.1/accounts/adunits/insert

Check the client libraries' samples to make sure every required field is added to the request. 

Let us know if you find the problem.

Cheers,

Jose


---
Jose Alcérreca
Developer Relations


Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Reply all
Reply to author
Forward
0 new messages