雖然開發人員符記會連結至管理員帳戶,但不會提供該帳戶的存取權。開發人員權杖授予的是一般 API 存取權,帳戶層級存取權則透過 OAuth 設定。
OAuth 憑證 (用於存取 Google Ads 帳戶)
如要以有權存取 Google Ads 帳戶的 Google 帳戶使用者身分授權,您必須提供一組 OAuth 憑證。
一般來說,有兩種 OAuth 流程:桌面 (已安裝) 應用程式或網頁應用程式。兩者的主要差異在於,桌面應用程式必須開啟系統瀏覽器,並提供本機重新導向 URI 來處理 Google 授權伺服器的回應,而網頁應用程式可以重新導向任意第三方瀏覽器來完成授權,並將憑證傳回伺服器。程式庫也支援較不常用的服務帳戶流程。
# List all examples:
./gradlew-qlistExamples
# List examples in the 'basicoperations' subdirectory:
./gradlew-qlistExamples--subdirectory='basicoperations'# Search for examples where the description includes 'Performance Max':
./gradlew-qlistExamples--searchTerm='Performance Max'
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-05 (世界標準時間)。"],[[["\u003cp\u003eUtilize the client library with Apache Maven or Gradle, adding the required dependency to your project.\u003c/p\u003e\n"],["\u003cp\u003eObtain a developer token and OAuth credentials for API access and account authorization, following the appropriate flow based on your use case.\u003c/p\u003e\n"],["\u003cp\u003eConfigure the client library with your credentials using a configuration file, environment variables, or programmatically, ensuring proper setup.\u003c/p\u003e\n"],["\u003cp\u003eValidate your credentials by running the provided GetCampaigns example with your customer ID.\u003c/p\u003e\n"],["\u003cp\u003eExplore the examples package for additional use cases and functionalities, customizing parameters as needed.\u003c/p\u003e\n"]]],[],null,["\u003cbr /\u003e\n\n\u003cbr /\u003e\n\nWe recommend using the client library with Apache Maven or Gradle.\n\nCreate a new Maven/Gradle project\n\nCreate a new Maven or Gradle project in the IDE of your choice. Our artifacts\nare published to the [Maven central\nrepository](//search.maven.org/artifact/com.google.api-ads/google-ads).\n\nWe recommend using the [Google Ads API's Bill of Materials\n(BOM)](https://central.sonatype.com/artifact/com.google.api-ads/google-ads-bom)\nto manage dependency versions. This is the best way to avoid dependency\nconflicts with libraries like `Guava` and `GAX` that are also used by other\nframeworks. The BOM ensures that you use the exact versions of these\ndependencies that have been tested with the Google Ads client library.\n\nThe Maven dependency is: \n\n \u003c!-- Import the Bill of Materials (BOM) to ensure you're using compatible\n versions of all google-ads libraries. --\u003e\n \u003cdependencyManagement\u003e\n \u003cdependencies\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.api-ads\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-ads-bom\u003c/artifactId\u003e\n \u003cversion\u003e40.0.0\u003c/version\u003e\n \u003ctype\u003epom\u003c/type\u003e\n \u003cscope\u003eimport\u003c/scope\u003e\n \u003c/dependency\u003e\n \u003c/dependencies\u003e\n \u003c/dependencyManagement\u003e\n\n \u003c!-- Add the google-ads dependency, without a version. The version is\n managed by the BOM. --\u003e\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.api-ads\u003c/groupId\u003e\n \u003cartifactId\u003egoogle-ads\u003c/artifactId\u003e\n \u003c/dependency\u003e\n\nThe Gradle dependency is: \n\n // Import the Bill of Materials (BOM).\n implementation platform('com.google.api-ads:google-ads-bom:40.0.0')\n\n // Add the google-ads dependency, without a version.\n implementation 'com.google.api-ads:google-ads'\n\nYou can also [build from source](/google-ads/api/docs/client-libs/java/build-from-source). For the purpose of this\nguide, it's assumed that you have a project setup with the required dependencies\navailable.\n\nIf you are building from source, ensure that you enable annotation processing in\nyour IDE.\n\nDeclaring dependencies covered by the BOM\n\nThe [Google Ads API\nBOM](https://central.sonatype.com/artifact/com.google.api-ads/google-ads-bom)\nincludes version management for several common libraries, such as `Guava`,\n`Protobuf`, `GAX`, and `gRPC`. To avoid potential dependency conflicts, you\n**must not** specify a version when declaring dependencies that the BOM covers.\nThe BOM automatically manages the versions for these libraries, ensuring\ncompatibility.\n\nFor example, to declare the `Guava` dependency in Maven, use the following: \n\n \u003cdependency\u003e\n \u003cgroupId\u003ecom.google.guava\u003c/groupId\u003e\n \u003cartifactId\u003eguava\u003c/artifactId\u003e\n \u003c!-- NO VERSION SPECIFIED --\u003e\n \u003c/dependency\u003e\n\nAnd in Gradle: \n\n implementation 'com.google.guava:guava' // NO VERSION SPECIFIED\n\nBy omitting the version, you let the BOM manage it, which helps prevent issues\ncaused by incompatible dependency versions. Common indicators of dependency\nconflicts include `NoSuchMethodError` or `ClassNotFoundException`, which can\noften be resolved by ensuring all BOM-managed dependencies have no version\nspecified.\n\nObtain credentials to authenticate with the API\n\nAccess to the Google Ads API requires OAuth credentials and a Google Ads API developer\ntoken. This section explains what these are, how they're used, and how they're\nobtained.\n\nDeveloper token (for access to the API)\n\nThe [developer token](/google-ads/api/docs/get-started/dev-token) is linked to a manager\naccount and can be found in the [Google Ads web interface](//ads.google.com).\n\nAlthough the developer token is linked to a manager account, it doesn't provide\naccess to that account. Instead, the developer token grants access to\nthe API in general, and account level access is configured through OAuth.\n\nOAuth credentials (for access to Google Ads accounts)\n\nTo authorize as Google account users with access to Google Ads accounts, you\nmust provide a set of OAuth credentials.\n\nThere are two OAuth flows that are generally used: desktop (installed) app\nor web app. The main difference between the two is that desktop apps must\nopen the system browser and supply a local redirect URI to handle responses from\nGoogle's authorization server, whereas web apps can redirect an arbitrary\nthird-party browser to complete authorization and send the credentials back to\nyour server. The library also supports the less commonly used service account\nflow.\n\nIf you authorize using your own credentials (desktop app flow)\n: Refer to the [OAuth desktop app\n flow](/google-ads/api/docs/client-libs/java/oauth-desktop). This includes all the details you\n need for authorizing with your own credentials.\n\nIf you authorize as a third-party Google user (web flow)\n: Refer to the [OAuth web app flow](/google-ads/api/docs/client-libs/java/oauth-web). This\n gives an example of how to set up OAuth authorization for arbitrary third-party\n users.\n\nIf you authorize as a Google Apps Domain user (service account flow)\n: Refer to the [OAuth service account\n flow](/google-ads/api/docs/client-libs/java/oauth-service). This gives an example of how to\n set up OAuth authorization for Google Apps Domain users.\n\nIf your access to the Google Ads customer account is through a Google Ads manager\naccount, you must also specify a login customer ID as described below.\n\nLogin customer ID (for access to Google Ads accounts through a manager account) **Note:** A Google user can have access to many Google Ads accounts.\n\nOptionally, specify the customer ID of a manager account which gives access to\nthe serving account. This must be specified if your access to the customer\naccount is through a manager account. There is no need to specify all manager\naccounts on the path to the customer ID, only the topmost manager ID which you\nare using for access permissions. For more details, see the [related\ndocumentation](/google-ads/api/docs/concepts/call-structure#cid).\n\nConfigure the client library with your credentials\n\nYou can either configure the client library with a configuration file,\nenvironment variables, or programmatically. For this guide, we'll use the\nconfiguration file approach and focus on the desktop and web flows. Using a\nconfiguration file is generally a good approach if you only have one set of\ncredentials (for example, you manage accounts under a single manager).\n| **Note:** If you authenticate with many different sets of OAuth credentials (for example, third-party advertisers use your application) then it will generally be easier to configure the library with the builder. Refer to [this\n| example](https://github.com/googleads/google-ads-java/blob/HEAD/google-ads-examples/src/main/java/com/google/ads/googleads/examples/library/ConfigureClientLibraryProgrammatically.java) for details. The same concepts apply in both cases.\n\nCreate a file `~/ads.properties` with the following content: \n\n api.googleads.clientId=INSERT_CLIENT_ID_HERE\n api.googleads.clientSecret=INSERT_CLIENT_SECRET_HERE\n api.googleads.refreshToken=INSERT_REFRESH_TOKEN_HERE\n api.googleads.developerToken=INSERT_DEVELOPER_TOKEN_HERE\n\nReplace the placeholders with your credentials obtained in the previous step.\n\nAdditionally, if your refresh token is for a manager account, you should\nspecify the customer ID of this account as the login customer: \n\n api.googleads.loginCustomerId=INSERT_LOGIN_CUSTOMER_ID_HERE\n\nValidate the credentials\n\nTo ensure that everything is setup correctly, we'll run the [GetCampaigns\nexample](https://github.com/googleads/google-ads-java/blob/HEAD/google-ads-examples/src/main/java/com/google/ads/googleads/examples/basicoperations/GetCampaigns.java).\n\nFirst, navigate into the `google-ads-examples` directory. \n\n cd google-ads-examples\n\nThis example requires a `--customerId` parameter where the value is your\nGoogle Ads account customer ID without dashes.\n\nTo run with Gradle: \n\n ./gradlew -q runExample --example=\"basicoperations.GetCampaigns --customerId INSERT_CUSTOMER_ID_HERE\"\n\nExplore other examples\n\nThe [examples](https://github.com/googleads/google-ads-java/blob/HEAD/google-ads-examples/src/main/java/com/google/ads/googleads/examples) package in `google-ads-examples`\ncontains several useful examples. Most of the examples require\nparameters. You can either pass the parameters as arguments (recommended) or\nedit the `INSERT_XXXXX_HERE` values in the source code. To see a usage statement\nfor an example, pass `--help` as the only argument.\n\nWith Gradle: \n\n ./gradlew -q runExample --example=\"basicoperations.GetCampaigns --help\"\n\nYou can also use the `listExamples` task in Gradle to list all examples,\nexamples in a subdirectory, or examples where the description includes a search\nterm. \n\n # List all examples:\n ./gradlew -q listExamples\n # List examples in the 'basicoperations' subdirectory:\n ./gradlew -q listExamples --subdirectory='basicoperations'\n # Search for examples where the description includes 'Performance Max':\n ./gradlew -q listExamples --searchTerm='Performance Max'\n\n| **Note:** From the source code, you can ignore comments with `[START...]` and `[END...]` in them---they are specifically used by Google."]]