Use Service Account with GoogleAdsClient to make AdWordsSession
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?
It looks like Adwords does support service account Authentication
OAuth2 service accounts
The AdWords API allows service account access through G Suite domains.
Which means you need to have your Gsuite domain admin authorize your service account to access it.
The Code should look something like this to authorize a service account
/** * Initializes an adwords service object. * * @return An authorized adwords service object. * @throws IOException * @throws GeneralSecurityException */ private static AdWordsServices initializeAdWords() throws GeneralSecurityException, IOException { HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); GoogleCredential credential = GoogleCredential .fromStream(new FileInputStream(KEY_FILE_LOCATION)) .createScoped(AdwordsScopes.all()); // Construct the Analytics Reporting service object. return new AdWordsServices.Builder(httpTransport, JSON_FACTORY, credential) .setApplicationName(APPLICATION_NAME).build(); }Thank you for the reply. Can you show me how I can use this code with my example code?
Your code is intended for Oauth2 not service account, i am suggesting you use this instead of your code.
Could you please extend the example a little bit more? How I have to replace the Session object?
Why do you need the session object. I showed you how to create the adWordsServices all your requests go though that.
ok, can you extend your example a little more, please?