{"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"}}]}
{"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 支持稀疏更新。
在 mutate 请求中,忽略字段掩码中的某个字段表示 API 不会对其进行处理。如果您的应用检索一个对象,对其进行更改然后发回,那么可能是因为您对一个不支持更新的字段执行了写入操作。请查看参考文档中的字段说明,了解在何时或是否可以更新该字段。
[[["易于理解","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"]],["最后更新时间 (UTC):2025-09-05。"],[[["\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."]]