本頁面提供範例,說明如何使用 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
以下範例說明如何使用 .NET 用戶端程式庫呼叫 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}"); } } 定義 PostalAddress 物件,其中包含元件化地址欄位,例如 RegionCode、Locality 和 AddressLines。建構範例要求時,系統會使用這個 PostalAddress 建立 ValidateAddressRequest。接著呼叫 ValidateAddressAsync 方法提出要求,並輸出回應中的詳細資料,例如 ValidationGranularity 和 FormattedAddress。
provideValidationFeedback
以下範例說明如何使用 .NET 用戶端程式庫呼叫 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,將意見回饋連結至正確的交易。