Java 用戶端程式庫的基本用法如下:
建立新的 Maven 或 Gradle 專案
在您選擇的 IDE 中建立新的 Maven 或 Gradle 專案。我們的構件會發布至 Maven 中央存放區。
建議使用 Google Ads API 的物料清單 (BOM) 管理依附元件版本。
您也可以從來源建構。本指南假設您已設定專案,並提供必要的依附元件。
如果您是從來源建構,請務必在 IDE 中啟用註解處理。
設定 API 存取權
如要連線至 Google Ads API,您必須提供驗證和授權憑證。如要進一步瞭解必要憑證和如何設定用戶端程式庫,請參閱驗證和授權指南。
撥打第一通電話
設定憑證後,即可進行第一次 API 呼叫。
我們會執行 GetCampaigns 範例,驗證您的憑證並發出第一通電話。
首先,請前往 google-ads-examples 目錄。
cd google-ads-examples
這個範例需要 --customerId 參數,值為不含破折號的 Google Ads 帳戶客戶 ID。
如要使用 Gradle 執行,請按照下列步驟操作:
./gradlew -q runExample --example="basicoperations.GetCampaigns --customerId INSERT_CUSTOMER_ID_HERE"
查看其他範例
google-ads-examples 中的 examples 套件包含多個實用範例。大多數範例都需要參數。您可以將參數做為引數傳遞 (建議),或編輯原始碼中的 INSERT_XXXXX_HERE 值。如要查看使用聲明範例,請傳遞 --help 做為唯一引數。
使用 Gradle:
./gradlew -q runExample --example="basicoperations.GetCampaigns --help"
您也可以在 Gradle 中使用 listExamples 工作,列出所有範例、子目錄中的範例,或說明中包含搜尋字詞的範例。
# List all examples:
./gradlew -q listExamples
# List examples in the 'basicoperations' subdirectory:
./gradlew -q listExamples --subdirectory='basicoperations'
# Search for examples where the description includes 'Performance Max':
./gradlew -q listExamples --searchTerm='Performance Max'