This guide requires several prerequisite settings that were configured in previous steps. Start with Introduction if you haven't done so.
Make an API call
Select your client of choice for instructions on how to make an API call:
Java
The client library artifacts are published to the Maven central repository . Add the client library as a dependency to your project as follows:
The Maven dependency is:
<dependency>
<groupId>com.google.api-ads</groupId>
<artifactId>google-ads</artifactId>
<version>40.0.0</version>
</dependency>
The Gradle dependency is:
implementation
'
com
.
google
.
api
-
ads
:
google
-
ads
:
40.0.0
'
api.googleads.serviceAccountSecretsPath=JSON_KEY_FILE_PATH
api.googleads.developerToken=INSERT_DEVELOPER_TOKEN_HERE
api.googleads.loginCustomerId=INSERT_LOGIN_CUSTOMER_ID_HERE
Create a GoogleAdsClient
object as follows:
GoogleAdsClient
googleAdsClient
=
null
;
try
{
googleAdsClient
=
GoogleAdsClient
.
newBuilder
().
fromPropertiesFile
().
build
();
}
catch
(
FileNotFoundException
fnfe
)
{
System
.
err
.
printf
(
"Failed to load GoogleAdsClient configuration from file. Exception: %s%n"
,
fnfe
);
System
.
exit
(
1
);
}
catch
(
IOException
ioe
)
{
System
.
err
.
printf
(
"Failed to create GoogleAdsClient. Exception: %s%n"
,
ioe
);
System
.
exit
(
1
);
}
Next, run a campaign report using the GoogleAdsService.SearchStream
method to retrieve the
campaigns in your account. This guide doesn't cover the details of reporting
.
private
void
runExample
(
GoogleAdsClient
googleAdsClient
,
long
customerId
)
{
try
(
GoogleAdsServiceClient
googleAdsServiceClient
=
googleAdsClient
.
getLatestVersion
().
createGoogleAdsServiceClient
())
{
String
query
=
"SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id"
;
// Constructs the SearchGoogleAdsStreamRequest.
SearchGoogleAdsStreamRequest
request
=
SearchGoogleAdsStreamRequest
.
newBuilder
()
.
setCustomerId
(
Long
.
toString
(
customerId
))
.
setQuery
(
query
)
.
build
();
// Creates and issues a search Google Ads stream request that will retrieve all campaigns.
ServerStream<SearchGoogleAdsStreamResponse>
stream
=
googleAdsServiceClient
.
searchStreamCallable
().
call
(
request
);
// Iterates through and prints all of the results in the stream response.
for
(
SearchGoogleAdsStreamResponse
response
:
stream
)
{
for
(
GoogleAdsRow
googleAdsRow
:
response
.
getResultsList
())
{
System
.
out
.
printf
(
"Campaign with ID %d and name '%s' was found.%n"
,
googleAdsRow
.
getCampaign
().
getId
(),
googleAdsRow
.
getCampaign
().
getName
());
}
}
}
}
C#
The client library packages are published to the Nuget.org
repository
. Start by adding
a nuget reference to Google.Ads.GoogleAds
package.
dotnet
add
package
Google.Ads.GoogleAds
--version
18
.1.0
Create a GoogleAdsConfig
object with the relevant settings, and use it to
create a GoogleAdsClient
object.
GoogleAdsConfig
config
=
new
GoogleAdsConfig
()
{
DeveloperToken
=
"******"
,
OAuth2Mode
=
OAuth2Flow
.
SERVICE_ACCOUNT
,
OAuth2SecretsJsonPath
=
"PATH_TO_CREDENTIALS_JSON"
,
LoginCustomerId
=
******
};
GoogleAdsClient
client
=
new
GoogleAdsClient
(
config
);
Next, run a campaign report using the GoogleAdsService.SearchStream
method to retrieve the
campaigns in your account. This guide doesn't cover the details of reporting
.
public
void
Run
(
GoogleAdsClient
client
,
long
customerId
)
{
// Get the GoogleAdsService.
GoogleAdsServiceClient
googleAdsService
=
client
.
GetService
(
Services
.
V21
.
GoogleAdsService
);
// Create a query that will retrieve all campaigns.
string
query
=
@"SELECT
campaign.id,
campaign.name,
campaign.network_settings.target_content_network
FROM campaign
ORDER BY campaign.id"
;
try
{
// Issue a search request.
googleAdsService
.
SearchStream
(
customerId
.
ToString
(),
query
,
delegate
(
SearchGoogleAdsStreamResponse
resp
)
{
foreach
(
GoogleAdsRow
googleAdsRow
in
resp
.
Results
)
{
Console
.
WriteLine
(
"Campaign with ID {0} and name '{1}' was found."
,
googleAdsRow
.
Campaign
.
Id
,
googleAdsRow
.
Campaign
.
Name
);
}
}
);
}
catch
(
GoogleAdsException
e
)
{
Console
.
WriteLine
(
"Failure:"
);
Console
.
WriteLine
(
$"Message: {e.Message}"
);
Console
.
WriteLine
(
$"Failure: {e.Failure}"
);
Console
.
WriteLine
(
$"Request ID: {e.RequestId}"
);
throw
;
}
}
PHP
The client library packages are published to the Packagist
repository
. Change into
the root directory of your project and run the following command to install
the library and all its dependencies in the vendor/
directory of your
project's root directory.
composer
require
googleads/google-ads-php:31.0.0
Make a copy of the google_ads_php.ini
file from the GitHub repository and modify it to include your credentials.
[GOOGLE_ADS]
developerToken = "INSERT_DEVELOPER_TOKEN_HERE"
loginCustomerId = "INSERT_LOGIN_CUSTOMER_ID_HERE"
[OAUTH2]
jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE"
scopes = "https://www.googleapis.com/auth/adwords"
Create an instance of GoogleAdsClient
object.
$oAuth2Credential = (new OAuth2TokenBuilder())
->fromFile('/path/to/google_ads_php.ini')
->build();
$googleAdsClient = (new GoogleAdsClientBuilder())
->fromFile('/path/to/google_ads_php.ini')
->withOAuth2Credential($oAuth2Credential)
->build();
Next, run a campaign report using the GoogleAdsService.SearchStream
method to retrieve the
campaigns in your account. This guide doesn't cover the details of reporting
.
public static function runExample(GoogleAdsClient $googleAdsClient, int $customerId)
{
$googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();
// Creates a query that retrieves all campaigns.
$query = 'SELECT campaign.id, campaign.name FROM campaign ORDER BY campaign.id';
// Issues a search stream request.
/** @var GoogleAdsServerStreamDecorator $stream */
$stream = $googleAdsServiceClient->searchStream(
SearchGoogleAdsStreamRequest::build($customerId, $query)
);
// Iterates over all rows in all messages and prints the requested field values for
// the campaign in each row.
foreach ($stream->iterateAllElements() as $googleAdsRow) {
/** @var GoogleAdsRow $googleAdsRow */
printf(
"Campaign with ID %d and name '%s' was found.%s",
$googleAdsRow->getCampaign()->getId(),
$googleAdsRow->getCampaign()->getName(),
PHP_EOL
);
}
}