本页面提供了有关如何使用 Address Validation API 客户端库调用以下服务的示例:
安装客户端库
如需查看安装说明,请参阅 Address Validation API 客户端库。
身份验证
使用客户端库时,您可以使用应用默认凭据 (ADC) 进行身份验证。如需了解如何设置 ADC,请参阅为应用默认凭据提供凭据。如需了解如何将 ADC 与客户端库搭配使用,请参阅使用客户端库进行身份验证。
您还可以使用 API 密钥对客户端库进行身份验证,例如:
using Google.Maps.AddressValidation.V1;
using Google.Api.Gax.Grpc;
using Grpc.Core;
...
// Create settings to pass the API key as a header in every request.
var apiHeader = CallSettings.FromHeader("X-Goog-Api-Key", "API_KEY");
var defaultSettings = AddressValidationSettings.GetDefault();
var settings = new AddressValidationSettings
{
ValidateAddressSettings = defaultSettings.ValidateAddressSettings.MergedWith(apiHeader),
ProvideValidationFeedbackSettings = defaultSettings.ProvideValidationFeedbackSettings.MergedWith(apiHeader)
};
// Create a client builder with the custom settings.
AddressValidationClientBuilder builder = new AddressValidationClientBuilder
{
Settings = settings,
// Use SslCredentials to create a secure channel for API key authentication.
ChannelCredentials = new SslCredentials()
};
AddressValidationClient client = await builder.BuildAsync();
当您在应用中使用 API 密钥时,请确保其在存储和传输期间均安全无虞。公开泄露 API 密钥可能会导致您的账号产生意外费用。
本页中的示例使用应用默认凭据。
示例
validateAddress
以下示例展示了如何使用客户端库调用 validateAddress
。
using Google.Maps.AddressValidation.V1;
using Google.Type;
...
private static async Task CallAddressValidation()
{
// Create the Address Validation Client
AddressValidationClient client = await AddressValidationClient.CreateAsync();
// Define the request with the address to be validated
var request = new ValidateAddressRequest
{
Address = new PostalAddress
{
RegionCode = "US",
LanguageCode = "en",
PostalCode = "94043",
AdministrativeArea = "CA",
Locality = "Mountain View",
AddressLines = { "1600 Amphitheatre Parkway" }
}
};
try
{
// Call the API asynchronously
ValidateAddressResponse response = await client.ValidateAddressAsync(request);
// Process the results
Console.WriteLine($"Validation Granularity: {response.Result.Verdict.ValidationGranularity}");
Console.WriteLine($"Formatted Address: {response.Result.Address.FormattedAddress}");
Console.WriteLine($"Response ID: {response.ResponseId}");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
使用 RegionCode
、Locality
和 AddressLines
等组件化地址字段定义 PostalAddress
对象。构建示例请求时,此 PostalAddress
用于创建 ValidateAddressRequest
。然后,系统会调用 ValidateAddressAsync
方法来发出请求,并输出响应中的详细信息,例如 ValidationGranularity
和 FormattedAddress
。
provideValidationFeedback
以下示例展示了如何使用客户端库调用 provideValidationFeedback
。
using Google.Maps.AddressValidation.V1;
...
private static async Task ProvideValidationFeedback()
{
AddressValidationClient client = await AddressValidationClient.CreateAsync();
var feedbackRequest = new ProvideValidationFeedbackRequest
{
// Set the conclusion based on the user's choice. This exampels uses ValidatedVersionUsed
Conclusion = ProvideValidationFeedbackRequest.Types.ValidationConclusion.ValidatedVersionUsed,
// Provide the ID from the validation response.
ResponseId = "Response_ID"
};
try
{
Console.WriteLine("Sending feedback to the API");
// This call returns an empty response on success.
await client.ProvideValidationFeedbackAsync(feedbackRequest);
Console.WriteLine(" -> Feedback sent successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
创建 ProvideValidationFeedbackRequest
以发送有关地址验证序列最终结果的信息。该请求需要 Conclusion
来指定是使用用户的原始地址还是 Google 经过验证的地址。重要的是,您还必须提供从初始 ValidateAddressResponse
获取的 ResponseId
,以将反馈与正确的交易相关联。