Friday, July 26, 2013

Digest for adsense-api@googlegroups.com - 4 Messages in 2 Topics

Group: http://groups.google.com/group/adsense-api/topics

    Tony John <tonyjohn@gmail.com> Jul 24 11:36PM -0700  

    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)" <adsenseapiadvisor+jose@google.com> Jul 25 03:34AM -0700  

    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
     
     
    On Thursday, 25 July 2013 07:36:36 UTC+1, Tony John wrote:

     

    "Sérgio Gomes (AdSense API Team)" <adsenseapiadvisor+sergio@google.com> Jul 25 02:02AM -0700  

    Hi Tony,
     
    The resource you link to (Accounts.adunits) is exactly what you should be
    passing in the request body.
     
    Cheers,
    Sérgio
     
    ---
    Sérgio Gomes
    Developer Relations
     
    Google UK Limited
    Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W
    9TQ
    Registered in England Number: 3977902
     
     
    On Thursday, 25 July 2013 04:14:41 UTC+1, Tony John wrote:

     

--
You received this message because you are subscribed to the Google Groups "AdSense API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adsense-api+unsubscribe@googlegroups.com.
To post to this group, send email to adsense-api@googlegroups.com.
Visit this group at http://groups.google.com/group/adsense-api.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment