您向 Google Ads API 发送请求时,该请求可能会因各种原因而失败。例如,您可能提供了无效实参,或者您的账号已达到制作新广告系列的限额。在这种情况下,API 会返回错误,告知您出了什么问题。
本指南介绍了如何读取和处理 API 错误,以便您构建更强大的应用。
错误结构
如果您使用的是我们的某个客户端库,则 API 错误会显示为异常。这些异常包含有助于您了解错误发生原因的详细信息。
Google Ads API 以标准格式返回错误信息。如果发生错误,响应将包含一个 GoogleAdsFailure 对象。此对象包含一个单独的 GoogleAdsError 对象列表,每个对象都详细说明了一个特定错误。
每个 GoogleAdsError 对象都提供:
error_code:一种特定的错误代码,用于告知您错误类型,例如AuthenticationError.NOT_ADS_USER。message:直观易懂的错误原因说明。trigger:导致错误的值,例如“1234”。location:有关请求的哪个部分导致了错误的详细信息,例如特定字段名称。
除了错误列表之外,GoogleAdsFailure 还包含一个 requestId,它是导致错误的 API 请求的唯一标识符。
错误示例
以下是 JSON 格式的错误示例。此错误表示请求中缺少索引为 0 的 ad_group 的 name 字段。
{
"code": 3,
"message": "Request contains an invalid argument.",
"details": [
{
"@type": "type.googleapis.com/google.ads.googleads.v22.errors.GoogleAdsFailure",
"errors": [
{
"errorCode": {
"requestError": "REQUIRED_FIELD_MISSING"
},
"message": "Required field is missing",
"location": {
"fieldPathElements": [
{
"fieldName": "ad_group",
"index": 0
},
{
"fieldName": "name"
}
]
}
}
],
"requestId": "unique_request_id_12345"
}
]
}
如需详细了解 API 错误,请参阅我们的指南。
客户端库示例
以下部分展示了如何在各种客户端库中处理错误。
Java
try {
// Make an API call.
...
} catch (GoogleAdsException gae) {
// GoogleAdsException is the base class for most exceptions thrown by an API request.
// Instances of this exception have a message and a GoogleAdsFailure that contains a
// collection of GoogleAdsErrors that indicate the underlying causes of the
// GoogleAdsException.
System.err.printf(
"Request ID %s failed due to GoogleAdsException. Underlying errors:%n",
gae.getRequestId());
int i = 0;
for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) {
System.err.printf(" Error %d: %s%n", i++, googleAdsError);
}
}
C#
try
{
// Make an API call.
...
}
catch (GoogleAdsException e)
{
Console.WriteLine($"Request with ID '{e.RequestId}' has failed.");
Console.WriteLine("Google Ads failure details:");
foreach (GoogleAdsError error in e.Failure.Errors)
{
Console.WriteLine($"{error.ErrorCode}: {error.Message}");
}
}
PHP
try {
// Make an API call.
...
} catch (GoogleAdsException $googleAdsException) {
printf(
"Request with ID '%s' has failed.%sGoogle Ads failure details:%s",
$googleAdsException->getRequestId(),
PHP_EOL,
PHP_EOL
);
foreach ($googleAdsException->getGoogleAdsFailure()->getErrors() as $error) {
/** @var GoogleAdsError $error */
printf(
"\t%s: %s%s",
$error->getErrorCode()->getErrorCode(),
$error->getMessage(),
PHP_EOL
);
}
}
Python
try:
# Make an API call.
...
except GoogleAdsException as ex:
print(
f"Request with ID '{ex.request_id}' failed with status "
f"'{ex.error.code().name}' and includes the following errors:"
)
for error in ex.failure.errors:
print(f"\tError with message '{error.message}' and code '{error.error_code}'.")
Ruby
begin
# Make an API call.
...
rescue Google::Ads::GoogleAds::Errors::GoogleAdsError => e
puts "API call failed with request ID: #{e.request_id}"
e.failure.errors.each do |error|
puts "\t#{error.error_code}: #{error.message}"
end
end
Perl
# Try sending a mutate request to add the ad group ad.
...
if ($response->isa("Google::Ads::GoogleAds::GoogleAdsException")) {
printf "Google Ads failure details:\n";
foreach my $error (@{$response->get_google_ads_failure()->{errors}}) {
printf "\t%s: %s\n", [keys %{$error->{errorCode}}]->[0], $error->{message};
}
}
如何捕获日志
如需排查错误,请捕获 Google Ads API 服务器返回的错误日志并检查其内容。按照以下说明启用日志记录并捕获 API 日志。
Java
如需查看相关说明,请参阅 Java 客户端库日志记录指南。
C#
您可以在 Main 方法中添加以下代码行,以在进行任何 API 调用之前初始化日志记录。这样可确保该库为您的应用发出的所有 API 调用生成日志。
using Google.Ads.GoogleAds.Util;
...
// Detailed logs.
TraceUtilities.Configure(TraceUtilities.DETAILED_REQUEST_LOGS_SOURCE,
"/path/to/your/logs/details.log", System.Diagnostics.SourceLevels.All);
// Summary logs.
TraceUtilities.Configure(TraceUtilities.SUMMARY_REQUEST_LOGS_SOURCE,
"/path/to/your/logs/summary.log", System.Diagnostics.SourceLevels.All);
如需了解其他选项,请参阅 .NET 库日志记录指南。
PHP
您可以在客户端库的 google_ads_php.ini 文件中设置日志记录配置。将 logLevel 设置为 NOTICE,开始捕获详细的错误日志。
[LOGGING]
; Optional logging settings.
logFilePath = "path/to/your/file.log"
logLevel = "NOTICE"
如需了解相关说明,请参阅 PHP 客户端库日志记录指南。
Python
您可以在客户端库的 google-ads.yaml 文件中设置日志记录配置。将日志记录级别设置为 DEBUG,即可开始捕获详细的错误日志。
如需了解其他选项,请参阅 Python 库日志记录指南。
Ruby
您可以在客户端库的 google_ads_config.rb 文件中设置日志记录配置。将日志记录级别设置为 INFO,即可开始捕获详细的错误日志。
如需了解其他选项,请参阅 Ruby 库日志记录指南。
Perl
如需初始化日志记录,请在 Perl 脚本中添加以下代码行,然后再进行任何 API 调用。
Google::Ads::GoogleAds::Logging::GoogleAdsLogger::enable_all_logging();
如需了解其他选项,请参阅 Perl 库日志记录指南。
curl
默认情况下,curl 会将失败的响应输出到 stderr。
如何处理错误
如果您遇到错误,请按以下步骤操作:
- 捕获异常并捕获日志:首先捕获异常,并可以选择性地捕获 API 日志。
- 检查
errors列表:查看GoogleAdsFailure对象中的每个GoogleAdsError。error_code和message会告诉您出了什么问题。 - 检查
location值:location字段可帮助您查明请求中出现问题的具体位置。 - 查阅文档:对于特定的错误代码,请查看常见错误页面或完整的错误代码参考,详细了解相应错误及修正方法。
- 调整请求:根据错误消息,更正 API 请求。例如,如果您看到
REQUIRED_FIELD_MISSING,请务必在请求中提供该字段。 - 记录
request_id:如果您无法找出解决错误的方法,并且需要联系支持团队,请提供失败请求的完整请求和响应日志。请务必添加request_id。此 ID 可帮助 Google 工程师在 Google Ads API 服务器日志中找到失败的请求详细信息,并调查您的问题。