Posts tagged with c#

I want to get Device Targeting Information from Google Ads Api as show in below screen shot:

Currently from what i get from the documentation i am doing this :

string  query = $@"SELECT campaign_criterion.device.type FROM campaign_criterion";       // also tried this query query = $@"SELECT campaign.id, campaign_criterion.device. FROM Device WHERE campaign.id =           {campaignId}";            PagedEnumerable<SearchGoogleAdsResponse, GoogleAdsRow> result =               googleAdsService.Search(customerId.ToString(), query);            foreach (GoogleAdsRow criterionRow in result)             {                 DeviceInfo device = criterionRow.CampaignCriterion.Device;             } 

I have randomly tried many other but i always see NULL in Device i have been able to successfully get Keywords , Ad Schedule View and works fine but cannot seem to get this working any help is appreciated.

This question is for those who are working with Google AdWords.

In Google AdWords, we have these account permissions

Is there any way to check if a user has read-only, standard or admin permissions via API? In ManagedCustomerService we have canManageClients, is this the admin? What about ready only and standard permissions?

I'm doing azure function which should regularly get ad reports from Google Ads API and save it to CSV. Copying code from Google documentation left me with this

public static void Run([TimerTrigger("0 22 12 * * *")] TimerInfo myTimer, ILogger log)     {         log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");         RunRequest(new GoogleAdsClient(new GoogleAdsConfig() {              DeveloperToken = "/*token*/",             OAuth2Mode = OAuth2Flow.SERVICE_ACCOUNT,             OAuth2PrnEmail = "/*service account email*/",             OAuth2SecretsJsonPath = "/*service account json*/"         }), "/*client id*/", log);     } public static void RunRequest(GoogleAdsClient client, string customerId, ILogger log)     {         // Get the GoogleAdsService.         GoogleAdsServiceClient googleAdsService = client.GetService(             Services.V5.GoogleAdsService);         // Create the query.         string query = @"/*request*/";         try         {             // Issue a search request.             googleAdsService.SearchStream(customerId, query,                 delegate (SearchGoogleAdsStreamResponse resp)                 {                     using var writer = new StreamWriter($"reports\\report{DateTime.Now}.csv");                     using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);                     csv.WriteRecords(Report.BuildReports(resp.Results));                 }             );         }         catch (GoogleAdsException e)         {             log.LogInformation("Failure:");             log.LogInformation($"Message: {e.Message}");             log.LogInformation($"Failure: {e.Failure}");             log.LogInformation($"Request ID: {e.RequestId}");             throw;         }     } 

Executing this code gives me an exception with this content:

"Status(StatusCode="Unauthenticated", Detail="Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project."

As I understand I don't need OAuth2 access token when using service account. How to fix this problem, what am I missing?

I am trying to PAUSE the Ads with the AdId and AdGroupID. I have successfully paused an AdGroup but i want to pause Ads individually. Is this possible on Adwords API. I tried the code below but It seems it only works on AdGroup level. Also checked the AdService but seems that there is no option to edit the Status. I am using Ads.AdWords.v201809 Thanks in advance

    public void googleEnableDisableAds(AdWordsUser user, long adGroupId, long AdID, AdGroupAdStatus AdStatus)     {         using (AdGroupAdService adGroupAdService =              (AdGroupAdService)user.GetService(AdWordsService.v201809.AdGroupAdService))         {             List<AdGroupAdOperation> operations = new List<AdGroupAdOperation>();             // Create the expanded text ad.             ExpandedTextAd expandedTextAd = new ExpandedTextAd             {                 //CR[i].                 id = AdID             };             AdGroupAd expandedTextAdGroupAd = new AdGroupAd             {                 adGroupId = adGroupId,                 ad = expandedTextAd,                 // Optional: Set the status.                 status = AdStatus             };             // Create the operation.             AdGroupAdOperation operation = new AdGroupAdOperation             {                 @operator = Operator.SET,                 operand = expandedTextAdGroupAd             };             operations.Add(operation);             AdGroupAdReturnValue retVal = null;             try             {                 if (operations.Count > 0)                 {                     // Create the ads.                     retVal = adGroupAdService.mutate(operations.ToArray());                     // Display the results.                     if (retVal != null && retVal.value != null)                     {                         foreach (AdGroupAd adGroupAd in retVal.value)                         {                             ExpandedTextAd newAd = adGroupAd.ad as ExpandedTextAd;                             Console.WriteLine(                                 "Expanded text ad with ID '{0}' and headline '{1} - {2}' " +                                 "was added.", newAd.id, newAd.headlinePart1, newAd.headlinePart2);                             //adGroupId                         }                     }                     else                     {                         Console.WriteLine("No expanded text ads were created.");                     }                 }                 adGroupAdService.Close();             }             catch (Exception e)             {                 throw new System.ApplicationException("Failed to create expanded text ad.", e);             }         }     }