Posts tagged with google-api-java-client

I want to use Google Ads API with service account I managed to create a session using this Java code configuration:

ClassLoader classLoader = this.getClass().getClassLoader();         File configFile = new File(classLoader.getResource("ads.properties").getFile());         GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder()                 .fromEnvironment()                 .fromPropertiesFile(configFile)                 .build();         GoogleAdsServiceClient googleAdsServiceClient = googleAdsClient.getLatestVersion().createGoogleAdsServiceClient(); 

I want to use this connection to make a request using this code:

AdWordsSession session = null;         try {             // Generate a refreshable OAuth2 credential.             Credential oAuth2Credential = new OfflineCredentials.Builder()                             .forApi(Api.ADWORDS)                             .fromFile()                             .build()                             .generateCredential();             // Construct an AdWordsSession.             session =                     new AdWordsSession.Builder().fromFile().build();         } catch (ConfigurationLoadException cle) {             System.err.printf(                     "Failed to load configuration from the %s file. Exception: %s%n",                     DEFAULT_CONFIGURATION_FILENAME, cle);             return;         } catch (ValidationException ve) {             System.err.printf(                     "Invalid configuration in the %s file. Exception: %s%n",                     DEFAULT_CONFIGURATION_FILENAME, ve);             return;         } catch (OAuthException oe) {             System.err.printf(                     "Failed to create OAuth credentials. Check OAuth settings in the %s file. "                             + "Exception: %s%n",                     DEFAULT_CONFIGURATION_FILENAME, oe);             return;         }         AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();         try {             runExample(adWordsServices, session);         } catch (ApiException apiException) {             System.err.println("Request failed due to ApiException. Underlying ApiErrors:");             if (apiException.getErrors() != null) {                 int i = 0;                 for (ApiError apiError : apiException.getErrors()) {                     System.err.printf("  Error %d: %s%n", i++, apiError);                 }             }         } catch (RemoteException re) {             System.err.printf(                     "Request failed unexpectedly due to RemoteException: %s%n", re);         } 

AdWordsServicesInterface adWordsServices, AdWordsSession session) throws RemoteException { // Get the TrafficEstimatorService. TrafficEstimatorServiceInterface trafficEstimatorService = adWordsServices.get(session, TrafficEstimatorServiceInterface.class);

Full source: https://developers.google.com/adwords/api/docs/samples/java/basic-operations

Do you know how I can use service account into the above code?

I already have a campaign budget which i want to update using google AdWords API.

   BudgetServiceInterface budgetService =         adWordsServices.get(session, BudgetServiceInterface.class);     // Create a budget, which can be shared by multiple campaigns.     Budget sharedBudget = new Budget();     sharedBudget.setName("Interplanetary Cruise #" + System.currentTimeMillis());     Money budgetAmount = new Money();     budgetAmount.setMicroAmount(50_000_000L);     sharedBudget.setAmount(budgetAmount);     sharedBudget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);     BudgetOperation budgetOperation = new BudgetOperation();     budgetOperation.setOperand(sharedBudget);     budgetOperation.setOperator(Operator.ADD);     // Add the budget     Long budgetId =         budgetService.mutate(new BudgetOperation[] {budgetOperation}).getValue(0).getBudgetId();     // Get the CampaignService.     CampaignServiceInterface campaignService =         adWordsServices.get(session, CampaignServiceInterface.class); 

Assiging budgetId to campaign

    Budget budget = new Budget();     budget.setBudgetId(budgetId);     campaign.setBudget(budget); 

This will unassign existing campaign budget and assign new campaign budget to the campaign ( campaign budget is not removed and it still exist in google ads) but I want to update the budget name and budget amount of existing campaign budget rather than assigning new campaign budget.