Export (0) Print
Expand All

Editorial Review and Appeals in C#

The following example shows how to find keywords that have been editorially rejected, print the reasons for the rejections, and then appeal the rejections using the following Campaign Management service operations.

This example has been developed and run within the environment described in Getting Started Using C# and Visual Basic with Bing Ads Services.

System_CLiX_note Note

This example uses the UserName and Password elements for authentication. For Managing User Authentication with OAuth, replace the UserName and Password elements with the AuthenticationToken, which is your OAuth access token.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

// BingAds.CampaignManagement is the application-defined namespace of 
// the service reference used for this example. 

using AppealKeywordRejections.BingAds.CampaignManagement;

namespace AppealKeywordRejections
{
    class Program
    {
        private static CampaignManagementServiceClient service = null;

        // API credentials.

        private static String m_password = "<passwordgoeshere>";
        private static String m_username = "<usernamegoeshere>";
        private static String m_token = "<devtokengoeshere>";

        // Advertiser's account ID and customer ID.

        private static long m_accountId = <accountidgoeshere>;
        private static long m_customerId = <customeridgoeshere>;

        // The ID of the ad group that contains the keywords to appeal.

        private static long m_adGroupId = <adgroupidgoeshere>; 


        static void Main(string[] args)
        {
            long[] ids = null;
            EditorialReasonCollection[] collections = null;

            try
            {
                service = new CampaignManagementServiceClient();

                ids = GetKeywordsThatFailedReview(m_accountId, m_adGroupId);

                if (null == ids)
                {
                    Console.WriteLine("Ad group {0} does not contain keywords that failed editorial review.",
                        m_adGroupId);
                }
                else
                {
                    collections = GetReasonsForFailures(m_customerId, m_accountId, ids);

                    foreach (EditorialReasonCollection collection in collections)
                    {
                        Console.WriteLine("Keyword {0} failed editorial review for the following reasons.",
                            collection.AdOrKeywordId);

                        foreach (EditorialReason reason in collection.Reasons)
                        {
                            Console.WriteLine("  Reason code: " + reason.ReasonCode);
                            Console.WriteLine("  Flagged ad component: " + reason.Location);
                            Console.WriteLine("  Flagged term: " + reason.Term);
                            Console.Write("  Publisher countries: ");

                            int j = 1;
                            int length = reason.PublisherCountries.Length;
                            foreach (string country in reason.PublisherCountries)
                            {
                                Console.Write("{0}{1}", country, (j++ < length) ? ", " : "\n");
                            }
                        }

                        if (AppealStatus.Appealable == collection.AppealStatus)
                        {
                            Console.WriteLine("Appealing review.");
                            string justification = "My justification text.";
                            AppealReview(m_accountId, new long[] { collection.AdOrKeywordId }, justification);
                        }
                        else if (AppealStatus.AppealPending == collection.AppealStatus)
                        {
                            Console.WriteLine("Review is being appealed.");
                        }
                        else
                        {
                            Console.WriteLine("Review cannot be appealed.");
                        }

                        Console.WriteLine();
                    }
                }

                service.Close();
            }
            catch (CommunicationException e)
            {
                Console.WriteLine(e.Message);

                if (null != e.InnerException)
                {
                    Console.WriteLine("\n" + e.InnerException.Message);
                }

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (TimeoutException e)
            {
                Console.WriteLine(e.Message);

                if (service != null)
                {
                    service.Abort();
                }
            }
            catch (Exception e)
            {
                // Ignore fault exceptions that we already caught.

                if (e.InnerException is FaultException)
                {
                    ;
                }
                else
                {
                    Console.WriteLine(e.Message);
                }


                if (service != null)
                {
                    service.Abort();
                }
            }
        }

        // Gets the IDs of the keywords that failed editorial review.

        static long[] GetKeywordsThatFailedReview(long accountId, long adGroupId)
        {
            GetKeywordsByEditorialStatusRequest request = new GetKeywordsByEditorialStatusRequest();
            GetKeywordsByEditorialStatusResponse response = null;
            long[] ids = null;
            int count = 0;

            // Set the header information.

            request.CustomerAccountId = accountId.ToString();
            request.DeveloperToken = m_token;
            request.UserName = m_username;
            request.Password = m_password;

            // Set the request information.

            request.AdGroupId = adGroupId;
            request.EditorialStatus = KeywordEditorialStatus.Disapproved;

            try
            {
                response = service.GetKeywordsByEditorialStatus(request);

                // Just need the IDs.

                if ((count = response.Keywords.Length) > 0)
                {
                    ids = new long[count];

                    for (int i = 0; i < count; i++)
                    {
                        ids[i] = (long)response.Keywords[i].Id;
                    }
                }
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("GetKeywordsByEditorialStatus failed with the following faults:\n");

                foreach (AdApiError error in fault.Detail.Errors)
                {
                    if (105 == error.Code) //  InvalidCredentials
                    {
                        Console.WriteLine("The specified credentials are not valid " +
                            "or the account is inactive.");
                    }
                    else
                    {
                        Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                            error.ErrorCode, error.Code, error.Message, error.Detail);
                    }
                }

                throw new Exception("", fault);
            }
            catch (FaultException<ApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("GetKeywordsByEditorialStatus failed with the following faults:\n");

                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    switch (error.Code)
                    {
                        case 1201: //  CampaignServiceInvalidAdGroupId
                            Console.WriteLine("The ad group ID is not valid.");
                            break;

                        default:
                            Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                                error.ErrorCode, error.Code, error.Message, error.Details);
                            break;
                    }
                }

                foreach (BatchError error in fault.Detail.BatchErrors)
                {
                    switch (error.Code)
                    {
                        default:
                            Console.WriteLine("Error code: {0} ({1})\nIndex: {2}\nMessage: {3}\nDetail: {4}\n",
                                error.ErrorCode, error.Code, error.Index, error.Message, error.Details);
                            break;
                    }
                }

                throw new Exception("", fault);
            }

            return ids;
        }

        // Gets the reasons why the keywords failed editorial review.

        static EditorialReasonCollection[] GetReasonsForFailures(long customerId, long accountId, long[] ids)
        {
            GetEditorialReasonsByIdsRequest request = new GetEditorialReasonsByIdsRequest();
            GetEditorialReasonsByIdsResponse response = null;

            // Set the header information.

            request.CustomerId = customerId.ToString();
            request.CustomerAccountId = accountId.ToString();
            request.DeveloperToken = m_token;
            request.UserName = m_username;
            request.Password = m_password;

            // Set the request information.

            request.AccountId = accountId;
            request.EntityIds = ids;
            request.EntityType = EntityType.Keyword;

            try
            {
                response = service.GetEditorialReasonsByIds(request);
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("GetEditorialReasonsByIds failed with the following faults:\n");

                foreach (AdApiError error in fault.Detail.Errors)
                {
                    if (105 == error.Code) //  InvalidCredentials
                    {
                        Console.WriteLine("The specified credentials are not valid " +
                            "or the account is inactive.");
                    }
                    else
                    {
                        Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                            error.ErrorCode, error.Code, error.Message, error.Detail);
                    }
                }

                throw new Exception("", fault);
            }
            catch (FaultException<ApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("GetEditorialReasonsByIds failed with the following faults:\n");

                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    switch (error.Code)
                    {
                        case 1029: //
                            Console.WriteLine("The customer ID header element cannot be null or empty.\n");
                            break;

                        default:
                            Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                                error.ErrorCode, error.Code, error.Message, error.Details);
                            break;
                    }
                }

                foreach (BatchError error in fault.Detail.BatchErrors)
                {
                    switch (error.Code)
                    {
                        default:
                            Console.WriteLine("Error code: {0} ({1})\nIndex: {2}\nMessage: {3}\nDetail: {4}\n",
                                error.ErrorCode, error.Code, error.Index, error.Message, error.Details);
                            break;
                    }
                }

                throw new Exception("", fault);
            }

            return response.EditorialReasons;
        }

        // Appeals the editorial rejections.

        static void AppealReview(long accountId, long[] ids, string justification)
        {
            AppealEditorialRejectionsRequest request = new AppealEditorialRejectionsRequest();
            AppealEditorialRejectionsResponse response = null;

            // Set the header information.

            request.CustomerAccountId = accountId.ToString();
            request.DeveloperToken = m_token;
            request.UserName = m_username;
            request.Password = m_password;

            // Set the request information.

            request.EntityIds = ids;
            request.EntityType = EntityType.Keyword;
            request.JustificationText = justification;

            try
            {
                response = service.AppealEditorialRejections(request);
            }
            catch (FaultException<AdApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("AppealEditorialRejections failed with the following faults:\n");

                foreach (AdApiError error in fault.Detail.Errors)
                {
                    if (105 == error.Code) //  InvalidCredentials
                    {
                        Console.WriteLine("The specified credentials are not valid " +
                            "or the account is inactive.");
                    }
                    else
                    {
                        Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                            error.ErrorCode, error.Code, error.Message, error.Detail);
                    }
                }

                throw new Exception("", fault);
            }
            catch (FaultException<ApiFaultDetail> fault)
            {
                // Log this fault.

                Console.WriteLine("AppealEditorialRejections failed with the following faults:\n");

                foreach (OperationError error in fault.Detail.OperationErrors)
                {
                    switch (error.Code)
                    {
                        default:
                            Console.WriteLine("Error code: {0} ({1})\nMessage: {2}\nDetail: {3}\n",
                                error.ErrorCode, error.Code, error.Message, error.Details);
                            break;
                    }
                }

                foreach (BatchError error in fault.Detail.BatchErrors)
                {
                    switch (error.Code)
                    {
                        default:
                            Console.WriteLine("Error code: {0} ({1})\nIndex: {2}\nMessage: {3}\nDetail: {4}\n",
                                error.ErrorCode, error.Code, error.Index, error.Message, error.Details);
                            break;
                    }
                }

                throw new Exception("", fault);
            }
        }
    }
}

Community Additions

Show:
© 2014 Microsoft