Java 用戶端程式庫的基本用法如下:
建立新的 Maven 或 Gradle 專案
在您選擇的 IDE 中建立新的 Maven 或 Gradle 專案。構件會發布至 Maven Central 存放區。
使用 Google Ads API 物料清單 (BOM) 管理依附元件版本。
如要執行 google-ads-examples 模組中包含的預先封裝程式碼範例,請從 GitHub 複製 google-ads-java 存放區,或從來源建構。如果您是從來源建構,請確保在 IDE 中啟用註解處理。
設定 API 存取權
如要連線至 Google Ads API,請提供驗證和授權憑證。如要進一步瞭解必要憑證和如何設定用戶端程式庫,請參閱「驗證和授權指南」。
發起第一通電話
設定憑證後,即可進行第一次 API 呼叫。
如要驗證憑證並使用複製的 google-ads-java 存放區發出第一通呼叫,請執行 GetCampaigns 範例。
首先,請前往 google-ads-examples 目錄:
cd google-ads-examples
這個範例需要 --customerId 參數,值為不含連字號的 Google Ads 帳戶客戶 ID (-)。
如要使用 Gradle 執行,請按照下列步驟操作:
./gradlew -q runExample \
--example="basicoperations.GetCampaigns --customerId INSERT_CUSTOMER_ID_HERE"
如要使用 Maven 執行:
mvn exec:java \
-Dexec.mainClass="com.google.ads.googleads.examples.basicoperations.GetCampaigns" \
-Dexec.args="--customerId INSERT_CUSTOMER_ID_HERE"
查看其他範例
google-ads-examples 中的 examples 套件包含多個實用範例。大多數範例都需要參數。您可以將參數當做引數傳遞 (建議),也可以編輯原始碼中的 INSERT_..._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'