疑難排解
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
ondemand_video
影片:觀看 2019 年研討會的錯誤處理講座
錯誤可能是因為環境設定不正確、軟體有錯誤,或是使用者輸入無效資料。無論來源為何,您都需要排解問題,並修正程式碼或新增邏輯來處理使用者錯誤。本指南將說明如何運用一些最佳做法,排解 Google Ads API 的錯誤。
確保連線
請確認您有權存取 Google Ads API,且設定正確無誤。如果回應傳回任何 HTTP 錯誤,請務必仔細處理這些錯誤,並確認您是從程式碼連線至要使用的服務。
您的憑證會嵌入要求中,以便服務驗證您的身分。請熟悉 Google Ads API 要求和回應的結構,尤其是要處理不使用用戶端程式庫的呼叫時。每個用戶端程式庫都會附上具體的操作說明,教您如何在設定檔中加入憑證 (請參閱用戶端程式庫的 README)。
確認您使用的憑證正確無誤。我們的快速入門導覽課程會逐步引導您取得正確的設定。舉例來說,下列回應失敗情形顯示使用者傳送的驗證憑證無效:
{
"error": {
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. Visit https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED",
"details": [
{
"@type": "type.googleapis.com/google.rpc.DebugInfo",
"detail": "Authentication error: 2"
}
]
}
}
如果按照上述步驟操作後仍無法解決問題,請開始排解 Google Ads API 錯誤。
判斷問題
Google Ads API 通常會以 JSON 失敗物件的形式回報錯誤,其中包含回應中的錯誤清單。這些物件會提供錯誤代碼,以及說明發生錯誤原因的訊息。這些是問題可能出在哪裡的初步信號。
{
"errors": [
{
"errorCode": { "fieldMaskError": "FIELD_NOT_FOUND" },
"message": "The field mask contained an invalid field: 'keyword/matchtype'.",
"location": { "operationIndex": "1" }
}
]
}
所有用戶端程式庫都會擲回例外狀況,封裝回應中的錯誤。擷取這些例外狀況,並在記錄或疑難排解畫面中列印訊息,是個不錯的開始。將這項資訊與應用程式中記錄的其他事件整合,即可大致瞭解可能觸發問題的原因。找出記錄中的錯誤後,您需要瞭解錯誤的意義。
研究錯誤
請參閱「常見錯誤」說明文件,瞭解最常遇到的錯誤。說明錯誤訊息、相關 API 參照,以及如何避免或處理錯誤。
如果常見錯誤說明文件未提及該錯誤,請參閱參考說明文件,並尋找錯誤字串。
搜尋我們的支援管道,與其他開發人員交流使用 API 的心得。其他人可能已遇到並解決您遇到的問題。
如果遇到任何未記錄的錯誤,請在論壇上回報。
如要排解驗證或帳戶限制問題,請前往 Google Ads 說明中心。Google Ads API 會沿用核心 Google Ads 產品的規則和限制。
有時,網誌文章會是排解應用程式問題時的實用參考資料。
研究錯誤後,請找出根本原因。
找出原因
查看例外狀況訊息,找出錯誤原因。查看回應後,請檢查要求,找出可能的原因。部分 Google Ads API 錯誤訊息會在 GoogleAdsError
的 location
欄位中包含 fieldPathElements
,指出要求中發生錯誤的位置。例如:
{
"errors": [
{
"errorCode": {"criterionError": "CANNOT_ADD_CRITERIA_TYPE"},
"message": "Criteria type can not be targeted.",
"trigger": { "stringValue": "" },
"location": {
"operationIndex": "0",
"fieldPathElements": [ { "fieldName": "keyword" } ]
}
}
]
}
排解問題時,可能是應用程式提供給 API 的資訊有誤。我們強烈建議使用互動式開發環境 (IDE),例如 Eclipse (免費的開放原始碼 IDE,主要用於開發 Java,但有其他語言的外掛程式),協助您進行偵錯。您可以設定中斷點,並逐行逐步執行程式碼。
請仔細檢查,確認要求與應用程式輸入內容相符 (例如,要求中可能沒有廣告活動名稱)。請務必傳送與要進行的更新相符的欄位遮罩,因為 Google Ads API 支援稀疏更新。在變動要求中從欄位遮罩省略欄位,表示 API 應保留該欄位。如果應用程式擷取物件、進行變更並傳回,您可能會寫入不支援更新的欄位。請參閱參考說明文件中的欄位說明,瞭解更新欄位時是否有任何限制。
如何取得協助
有時您可能無法自行找出並解決問題。
在論壇提問,可讓數千名開發人員看到您的問題,他們可能也曾遇到相同問題。
請盡量在查詢中提供相關資訊。
建議提供的資訊包括:
- 經過清理的 JSON 要求和回應。請務必移除開發人員權杖或 AuthToken 等機密資訊。
- 程式碼片段。如果您遇到特定語言的問題,或是需要 API 相關協助,請附上程式碼片段,說明您執行的操作。
- RequestId。如果要求是針對正式版環境提出,Google 開發人員關係團隊成員就能找到您的要求。建議您在記錄檔中,將 requestId 登錄為封裝回應錯誤的例外狀況屬性,並登錄比 requestId 本身更豐富的內容。
- 此外,執行階段/解譯器版本和平台等其他資訊,也有助於排解問題。
修正相關問題
您已找出問題並提出解決方案,現在可以進行變更,並針對測試帳戶 (建議) 或正式版 (如果錯誤只適用於特定正式版帳戶中的資料) 測試修正程式。
考慮分享
如果您在論壇中發布了有關先前未出現錯誤的問題,且已找到解決方案,請考慮將解決方案新增至該討論串。下次開發人員遇到相同問題時,就能立即解決。
後續步驟
您已解決這個問題,但您是否發現任何改善程式碼的方法,以避免再次發生這種情況?
建立一組良好的單元測試,有助於大幅提升程式碼品質和可靠性。此外,這項功能還能加快測試新變更的程序,確保新變更不會破壞先前的功能。此外,良好的錯誤處理策略也是找出所有必要資料以利排解問題的關鍵。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-26 (世界標準時間)。
[null,null,["上次更新時間:2025-08-26 (世界標準時間)。"],[[["\u003cp\u003eThis guide provides best practices for troubleshooting errors when using the Google Ads API, covering connectivity checks, error identification, and resolution strategies.\u003c/p\u003e\n"],["\u003cp\u003eYou can determine the problem by examining the JSON error objects in the API response, researching the error code in the documentation or support channels, and analyzing the request for potential causes.\u003c/p\u003e\n"],["\u003cp\u003eLeverage IDEs for debugging, ensure accurate request data, and consult the documentation for field restrictions to effectively locate the root cause of errors.\u003c/p\u003e\n"],["\u003cp\u003eIf independent troubleshooting proves insufficient, seek assistance through the Google Ads API forum, providing comprehensive information to facilitate resolution.\u003c/p\u003e\n"],["\u003cp\u003eAfter resolving the error, enhance your code with unit tests and robust error handling to prevent similar issues in the future.\u003c/p\u003e\n"]]],[],null,["# Troubleshooting\n\nondemand_video\n[Video: Check out the error handling talk from the 2019 workshop](https://www.youtube.com/watch?v=J9zVoWT7twk&list=None&start=1158)\n\nErrors can be caused by an incorrect environment setup, a bug in\nyour software, or invalid input from a user. No matter the source, you will\nneed to troubleshoot the issue and either fix your code or add logic to handle\nthe user error. This guide discusses some best practices when troubleshooting\nerrors from the Google Ads API.\n\nEnsuring connectivity\n---------------------\n\n1. Make sure you have access to the Google Ads API and a correct setup. If your\n response returns any HTTP errors, make sure you address those carefully and\n that you are reaching the services you intend to use from your code.\n\n2. Your credentials are embedded in your\n [request](/google-ads/api/docs/concepts/call-structure#request_headers) in order for the\n services to authenticate you. Familiarize yourself with the structure of Google Ads API\n requests and responses, especially if you are going to handle calls without\n using the client libraries. Each client library is shipped with specific\n instructions on how to include your credentials in the config file (consult the\n client library's README).\n\n3. Verify that you are using the correct credentials. Our\n [Quickstart](/google-ads/api/docs/start) takes you through the process of acquiring the\n correct set you need. For example, the following response failure shows that the\n user has sent invalid authentication credentials:\n\n {\n \"error\": {\n \"code\": 401,\n \"message\": \"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. Visit https://developers.google.com/identity/sign-in/web/devconsole-project.\",\n \"status\": \"UNAUTHENTICATED\",\n \"details\": [\n {\n \"@type\": \"type.googleapis.com/google.rpc.DebugInfo\",\n \"detail\": \"Authentication error: 2\"\n }\n ]\n }\n }\n\nIf you've followed these steps and are still having issues, it's time to dive\ninto troubleshooting the Google Ads API errors.\n\nDetermining the problem\n-----------------------\n\nThe Google Ads API generally reports errors as a JSON failure object, containing\na list of errors in the response. These objects provide an error code as well as\na message elaborating on why it occurred. They are your first signals of what\nthe problem might be. \n\n {\n \"errors\": [\n {\n \"errorCode\": { \"fieldMaskError\": \"FIELD_NOT_FOUND\" },\n \"message\": \"The field mask contained an invalid field: 'keyword/matchtype'.\",\n \"location\": { \"operationIndex\": \"1\" }\n }\n ]\n }\n\nAll our [client libraries](/google-ads/api/docs/client-libs) throw exceptions that\nencapsulate errors in the response. Capturing these exceptions and\nprinting out the messages in a log or a troubleshooting screen is a great way\nto start. Integrating this information with the other logged events in your\napplication offers a good overview of what might be triggering the problem.\nOnce you've identified the error in the logs, you'll need to figure out what\nit means.\n\nResearching the error\n---------------------\n\n1. Refer to our [Common Errors](/google-ads/api/docs/best-practices/common-errors)\n documentation, which covers the most frequently encountered errors. It\n describes the error message, relevant API references, and how to avoid or\n handle the error.\n\n2. If our common errors documentation doesn't specifically mention the error,\n consult our [reference documentation](/google-ads/api/reference/rpc) and look for the error string.\n\n3. Search our [support channels](/google-ads/api/support) to gain access to other\n developers sharing their experiences with the API. Someone else may have\n run into---and solved---the problem you are having.\n\n4. If you encounter any errors that are not documented, bring this to\n our attention on the forum.\n\n5. Go to the [Google Ads Help Center](//support.google.com/google-ads/)\n for help troubleshooting validation or account limit issues---the\n Google Ads API inherits the rules and limitations of the core Google Ads product.\n\n6. [Blog posts](//ads-developers.googleblog.com/search/label/google_ads_api)\n will on occasion be a good reference when troubleshooting your application.\n\nAfter researching the error, it's time to determine the root cause.\n\nLocating the cause\n------------------\n\nCheck the exception message to determine the cause of the error. After looking\nat the response, check the request for a possible cause. Some Google Ads API error\nmessages include a [`fieldPathElements`](/google-ads/api/reference/rpc/v21/ErrorLocation#field_path_elements%5B%5D)\nin the [`location`](/google-ads/api/reference/rpc/v21/GoogleAdsError#location) field of the\n[`GoogleAdsError`](/google-ads/api/reference/rpc/v21/GoogleAdsError), indicating where\nin the request the error occurred. For example: \n\n {\n \"errors\": [\n {\n \"errorCode\": {\"criterionError\": \"CANNOT_ADD_CRITERIA_TYPE\"},\n \"message\": \"Criteria type can not be targeted.\",\n \"trigger\": { \"stringValue\": \"\" },\n \"location\": {\n \"operationIndex\": \"0\",\n \"fieldPathElements\": [ { \"fieldName\": \"keyword\" } ]\n }\n }\n ]\n }\n\nWhen troubleshooting an issue, it may be that your application is providing the\nwrong information to the API. We strongly encourage the use of an Interactive\nDevelopment Environment (IDE) such as [Eclipse](//eclipse.org/) (a free\nand open source IDE which is primarily used to develop Java, but has plugins for\nother languages) to aid you in debugging. It lets you set breakpoints and step\nthrough your code line by line.\n\nDouble-check to make sure the request matches your application inputs (for\nexample the Campaign's name might not be making it to the request). Make sure\nyou send a\n[field mask](/protocol-buffers/docs/reference/google.protobuf#field-mask)\nthat matches the updates you want to make--the Google Ads API supports sparse updates.\nOmitting a field from the field mask in a mutate request indicates the API\nshould leave it alone. If your application retrieves an object, makes a change,\nand sends it back, you may be writing to a field that does not\nsupport updating. Check the field's description in the [reference\ndocumentation](/google-ads/api/reference/rpc) to see if there are any restrictions on when or if you\ncan update the field.\n\n### How to get help\n\nIt's not always possible to identify and solve the problem on your own.\nAsking on the [forum](//groups.google.com/g/adwords-api) exposes\nyour question to thousands of developers who may have had to deal with the same\nissue.\n\nTry to include as much information as you can in your queries.\nRecommended items include:\n\n- Sanitized JSON request and response. Make sure to remove sensitive information such as your developer token or AuthToken.\n- Code snippets. If you are having a language-specific problem or are requesting help working with the API, include a snippet of code to help explain what you are doing.\n- RequestId. This enables Google Developer Relations team members to locate your request if made against the production environment. We recommend registering in your logs the requestId included as a property in the exceptions that encapsulate response errors, as well as more context than requestId alone.\n- Additional information, such as runtime/interpreter version and platform can also be useful when troubleshooting.\n\nFixing the problem\n------------------\n\nNow that you've figured out the problem and come up with a solution, it's time\nto make your change and test the fix against a test account (preferred) or\nproduction (if the bug only applies to data in a specific production account).\n\n### Consider Sharing\n\nIf you have posted a question in the forum regarding an error that hadn't been\nsurfaced there before and you have found the solution, consider adding it to the\nthread. Next time a developer has the same problem they'll be able to solve it\nright away.\n\n### Next Steps\n\nNow that you've solved this problem, did you notice any ways to improve your\ncode to avoid this in the first place?\n\nCreating a good set of unit tests helps improve code quality and reliability\nconsiderably. It also speeds up the process of testing new changes to ensure\nthey didn't break previous functionality. A good error handling strategy is also\nkey in surfacing all the necessary data for troubleshooting."]]