Để thảo luận và đưa ra ý kiến phản hồi về các sản phẩm của chúng tôi, hãy tham gia kênh Discord chính thức của Google Ads trong máy chủ Cộng đồng quảng cáo và đo lường của Google.
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Hướng dẫn này thảo luận về cách truy cập vào API Google Ads bằng tài khoản dịch vụ.
Tài khoản dịch vụ là tài khoản thuộc về ứng dụng của bạn chứ không phải thuộc về người dùng cuối cá nhân. Tài khoản dịch vụ cho phép các hoạt động tương tác giữa máy chủ với máy chủ giữa một ứng dụng web và một dịch vụ của Google.
Ứng dụng của bạn gọi các API của Google thay cho tài khoản dịch vụ, vì vậy, người dùng không trực tiếp tham gia.
Tài khoản dịch vụ sử dụng quy trình OAuth 2.0 không yêu cầu sự cho phép của con người, thay vào đó sử dụng tệp khoá mà chỉ ứng dụng của bạn mới có thể truy cập.
Việc sử dụng tài khoản dịch vụ mang lại 2 lợi ích chính:
Quyền truy cập vào API của Google được thực hiện dưới dạng một bước định cấu hình, do đó, tránh được các vấn đề phức tạp liên quan đến các quy trình OAuth 2.0 khác yêu cầu người dùng tương tác.
Quy trình xác nhận OAuth 2.0 cho phép ứng dụng của bạn mạo danh người dùng khác nếu cần.
Có hai cách để uỷ quyền bằng tài khoản dịch vụ: trực tiếp hoặc bằng cách mạo danh (không nên dùng).
[Đề xuất] Uỷ quyền bằng quyền truy cập trực tiếp vào tài khoản
Trong lựa chọn này, bạn cấp cho tài khoản dịch vụ quyền truy cập trực tiếp vào tài khoản Google Ads của mình.
Tải khoá tài khoản dịch vụ xuống ở định dạng JSON và ghi lại mã nhận dạng cũng như email của tài khoản dịch vụ.
Đăng nhập vào tài khoản Google Ads với tư cách là quản trị viên. Chuyển đến mục Quản trị > Quyền truy cập và bảo mật.
Nhấp vào nút + trong thẻ Người dùng.
Nhập địa chỉ email của tài khoản dịch vụ vào hộp nhập Email. Chọn cấp truy cập tài khoản phù hợp rồi nhấp vào nút Thêm tài khoản. Xin lưu ý rằng các cấp truy cập Email và Quản trị viên không được hỗ trợ cho tài khoản dịch vụ.
Tài khoản dịch vụ được cấp quyền truy cập.
Cấu hình thư viện ứng dụng
Chọn thẻ tương ứng với ngôn ngữ lập trình của bạn để xem hướng dẫn về cách định cấu hình thư viện ứng dụng.
Java
Đặt đường dẫn JSON khoá riêng tư trong cấu hình của bạn. Nếu bạn đang sử dụng tệp ads.properties, hãy thêm nội dung sau:
Đặt đường dẫn JSON khoá riêng tư và mã nhận dạng tài khoản uỷ quyền trong cấu hình của bạn. Nếu bạn đang sử dụng tệp googleads.properties, hãy thêm nội dung sau:
jsonKeyFilePath=JSON_KEY_FILE_PATH
Nếu bạn đang sử dụng các biến môi trường, hãy thêm nội dung sau vào cấu hình hoặc môi trường Bash:
Trong lựa chọn này, bạn mạo danh một người dùng có quyền truy cập vào tài khoản Google Ads của bạn bằng tài khoản dịch vụ. Phương pháp này chỉ áp dụng cho khách hàng Google Workspace. Tài khoản dịch vụ chỉ có thể mạo danh người dùng (địa chỉ email) trong cùng một Google Workspace.
Điều kiện tiên quyết
Một miền Google Workspace mà bạn sở hữu, chẳng hạn như mydomain.com hoặc mybusiness.com
Mã của nhà phát triển API Google Ads và tài khoản thử nghiệm (không bắt buộc)
Người dùng Google Ads có quyền đối với tài khoản Google Ads mà bạn muốn truy cập.
Thiết lập quyền truy cập vào tài khoản dịch vụ
Vì tính năng mạo danh người dùng chỉ được kiểm soát ở cấp miền, nên khi sử dụng tài khoản dịch vụ và quy trình xác nhận bằng Google OAuth 2.0, bạn phải đăng ký miền của riêng mình với Google Workspace. Khi đó, ứng dụng và người dùng của ứng dụng có thể mạo danh bất kỳ người dùng nào trong miền.
Giờ đây, bạn có thể sử dụng tài khoản dịch vụ để truy cập vào tài khoản Google Ads của mình bằng quy trình xác nhận OAuth 2.0.
Cấu hình thư viện ứng dụng
Chọn thẻ tương ứng với ngôn ngữ lập trình của bạn để xem hướng dẫn về cách định cấu hình thư viện ứng dụng.
Java
Đặt đường dẫn JSON khoá riêng tư và mã nhận dạng tài khoản uỷ quyền trong cấu hình của bạn.
Nếu bạn đang sử dụng tệp ads.properties, hãy thêm nội dung sau:
Đặt đường dẫn JSON của khoá riêng tư và email được mạo danh trong cấu hình của bạn.
Nếu bạn đang sử dụng google-ads.yaml file, chuỗi YAML hoặc dict, hãy thêm nội dung sau:
Đặt đường dẫn JSON khoá riêng tư và mã nhận dạng tài khoản uỷ quyền trong cấu hình của bạn. Nếu bạn đang sử dụng tệp googleads.properties, hãy thêm nội dung sau:
Vì tài khoản dịch vụ có quyền kiểm soát uỷ quyền ở cấp miền cho miền Google Workspace của bạn, nên bạn cần bảo vệ tệp khoá cho phép tài khoản dịch vụ truy cập vào các dịch vụ của Google mà tài khoản đó được uỷ quyền. Điều này đặc biệt đúng vì tài khoản dịch vụ đó có khả năng mạo danh bất kỳ người dùng nào trong miền.
Một phương pháp hay khác là chỉ cho phép tài khoản dịch vụ truy cập vào bộ API tối thiểu bắt buộc. Đây là biện pháp phòng ngừa để giới hạn lượng dữ liệu mà kẻ tấn công có thể truy cập nếu tệp khoá của tài khoản dịch vụ bị xâm phạm.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-05 UTC."],[[["\u003cp\u003eThis guide explains how to use service accounts for server-to-server interactions with the Google Ads API, enabling app access without direct user involvement.\u003c/p\u003e\n"],["\u003cp\u003eService accounts offer simplified authorization and the ability to impersonate other users, though impersonation is a legacy approach and discouraged for security reasons.\u003c/p\u003e\n"],["\u003cp\u003eTwo authorization methods are detailed: direct access, granting the service account permissions to your Google Ads account, and impersonation (for Google Workspace users only), allowing the service account to act on behalf of a specific user.\u003c/p\u003e\n"],["\u003cp\u003eConfiguration instructions are provided for various client libraries (Java, .NET, Python, PHP, Ruby, Perl) to set up service account authentication.\u003c/p\u003e\n"],["\u003cp\u003eSecurity concerns are highlighted, emphasizing the importance of protecting the service account key file and limiting API access to minimize potential risks.\u003c/p\u003e\n"]]],[],null,["# Service Accounts\n\nThis guide discusses how to access the Google Ads API with service accounts.\n\nA [service account](/identity/protocols/OAuth2#serviceaccount) is an account\nthat belongs to your app instead of to an individual end user. Service accounts\nenable server-to-server interactions between a web app and a Google service.\nYour app calls Google APIs on behalf of the service account, so users aren't\ndirectly involved.\n\nService accounts employ an OAuth 2.0 flow that doesn't require human\nauthorization, using instead a key file that only your app can access.\n\nUsing service accounts provides two key benefits:\n\n- Authorization for Google API access is done as a configuration step, thus\n avoiding the complications associated with other OAuth 2.0 flows that require\n user interactions.\n\n- The OAuth 2.0 assertion flow lets your app impersonate other users if\n necessary.\n\nThere are two ways to authorize with service accounts: directly or\nwith impersonation (not recommended).\n\n\\[Recommended\\] Authorization with direct account access\n--------------------------------------------------------\n\nIn this option, you give the service account direct access to your Google Ads\naccount.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n### Account access setup\n\n1. Start by [creating a service account and credentials](/workspace/guides/create-credentials#service-account).\n\n Download the service account key in JSON format and note the service account\n ID and email.\n2. Sign in to your Google Ads account as an administrator. Navigate to **Admin \\\u003e Access and security**.\n\n3. Click the **+** button under the **Users** tab.\n\n4. Type the service account email into the **Email** input box. Select the\n appropriate level of account access level and click the **Add account**\n button. Note that Email and Admin access levels are not supported for service\n accounts.\n\n5. The service account is granted access.\n\n### Client library configuration\n\nSelect the tab corresponding to your programming language for instructions on\nhow to configure your client library. \n\n### Java\n\nSet the private key JSON path in your configuration. If you're using an\n`ads.properties` file, add the following: \n\n api.googleads.serviceAccountSecretsPath=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n\nSee the [configuration guide](/google-ads/api/docs/client-libs/java/config-file) for\nadditional details.\n\n### .NET\n\nConfigure the following keys in your `App.config / Web.config`. See the\n[configuration guide](/google-ads/api/docs/client-libs/dotnet/configuration) for\nadditional details. \n\n \u003cadd key=\"OAuth2Mode\" value=\"SERVICE_ACCOUNT\" /\u003e\n \u003cadd key=\"OAuth2SecretsJsonPath\" value=\"\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\" /\u003e\n\n### Python\n\nSet the private key JSON path in your configuration. If you're using a\n`google-ads.yaml file`, YAML string, or `dict`, add the following: \n\n json_key_file_path: \u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n\nIf you're using environment variables, add the following to your Bash\nconfiguration or environment: \n\n export GOOGLE_ADS_JSON_KEY_FILE_PATH=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n\n### PHP\n\nConfigure the following keys in your `google_ads_php.ini`. See the\n[configuration guide](/google-ads/api/docs/client-libs/php/configuration) for additional\ndetails. \n\n ; For service account flow.\n jsonKeyFilePath = \"\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\"\n scopes = \"https://www.googleapis.com/auth/adwords\"\n\n### Ruby\n\nConfigure the following keys in your [`google_ads_config.rb`](//github.com/googleads/google-ads-ruby/blob/HEAD/google_ads_config.rb#L29). \n\n c.keyfile = '\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e'\n\n### Perl\n\nSet the private key JSON path and delegate account ID in your\nconfiguration. If you're using a `googleads.properties` file, add the\nfollowing: \n\n jsonKeyFilePath=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n\nIf you're using environment variables, add the following to your Bash\nconfiguration or environment: \n\n export GOOGLE_ADS_JSON_KEY_FILE_PATH=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n\n\\[Not Recommended\\] Authorization using impersonation\n-----------------------------------------------------\n\n| **Caution:** This is a legacy approach that works only for Google Workspace users. Its use is highly discouraged; this documentation is maintained only for reference purposes. Newer users should use the [Authorization with direct account access](#direct) approach, which works for all users including Google Workspace users and is simpler to configure and use.\n\nIn this option, you impersonate a user who has access to your Google Ads account\nusing the service account. This approach works only for Google Workspace\ncustomers. A service account can only impersonate users (email addresses) in\nthe same [Google Workspace](//workspace.google.com/).\n\n### Prerequisites\n\n- A [Google Workspace domain](//workspace.google.com/) that you own such as\n `mydomain.com` or `mybusiness.com`\n\n- A Google Ads API developer token and optionally a test account\n\n- The [client library](/google-ads/api/docs/client-libs) for the language you're using\n\n- A Google API Console project that has been [configured for the\n Google Ads API](/google-ads/api/docs/oauth/cloud-project#enable_the_in_your_project)\n\n- A Google Ads user with permissions on the Google Ads account you want to access.\n\n### Service account access setup\n\nSince user impersonation is controlled only at the domain level, using service\naccounts and assertion flow with Google OAuth 2.0 requires you to have your own\ndomain registered with Google Workspace. Your app and its users can then\nimpersonate any user in the domain.\n\n1. Start by [creating a service account and credentials](/workspace/guides/create-credentials#service-account).\n\n Download the service account key in JSON format and note the service account\n ID.\n2. Share the service account ID and the Google Ads API scope\n (`https://www.googleapis.com/auth/adwords`) with your domain administrator.\n\n Request the domain administrator to delegate domain-wide authority to your\n service account.\n3. If you're the domain administrator, complete the [help center\n instructions](//support.google.com/a/answer/162106).\n\nYou can now use the service account to access your Google Ads account with the\nOAuth 2.0 assertion flow.\n\n### Client library configuration\n\nSelect the tab corresponding to your programming language for instructions on\nhow to configure your client library. \n\n### Java\n\nSet the private key JSON path and delegate account ID in your configuration.\nIf you're using an `ads.properties` file, add the following: \n\n api.googleads.serviceAccountSecretsPath=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n api.googleads.serviceAccountUser=\u003cvar translate=\"no\"\u003eIMPERSONATED_EMAIL\u003c/var\u003e\n\nSee the [configuration guide](/google-ads/api/docs/client-libs/java/config-file) for\nadditional details.\n\n### .NET\n\nConfigure the following keys in your `App.config / Web.config`. See the\nconfiguration guide for additional details. \n\n \u003cadd key=\"OAuth2Mode\" value=\"SERVICE_ACCOUNT\" /\u003e\n \u003cadd key=\"OAuth2SecretsJsonPath\" value=\"\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\" /\u003e\n\n \u003c!-- Supply the email address of the user to impersonate. --\u003e\n \u003cadd key=\"OAuth2PrnEmail\" value=\"\u003cvar translate=\"no\"\u003eIMPERSONATED_EMAIL\u003c/var\u003e\" /\u003e\n\n### Python\n\nSet the private key JSON path and impersonated email in your configuration.\nIf you're using a `google-ads.yaml file`, YAML string, or `dict`, add the\nfollowing: \n\n json_key_file_path: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eJSON_KEY_FILE_PATH\u003c/span\u003e\u003c/var\u003e\n impersonated_email: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eIMPERSONATED_EMAIL\u003c/span\u003e\u003c/var\u003e\n\nIf you're using environment variables, add the following to your Bash\nconfiguration or environment: \n\n export GOOGLE_ADS_JSON_KEY_FILE_PATH=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n export GOOGLE_ADS_IMPERSONATED_EMAIL=\u003cvar translate=\"no\"\u003eIMPERSONATED_EMAIL\u003c/var\u003e\n\n### PHP\n\nConfigure the following keys in your `google_ads_php.ini`. See the\n[configuration guide](/google-ads/api/docs/client-libs/php/configuration) for additional\ndetails. \n\n ; For service account flow.\n jsonKeyFilePath = \"\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\"\n scopes = \"https://www.googleapis.com/auth/adwords\"\n impersonatedEmail = \"\u003cvar translate=\"no\"\u003eIMPERSONATED_EMAIL\u003c/var\u003e\"\n\n### Ruby\n\nConfigure the following keys in your [`google_ads_config.rb`](//github.com/googleads/google-ads-ruby/blob/HEAD/google_ads_config.rb#L29). \n\n c.keyfile = '\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e'\n c.impersonate = '\u003cvar translate=\"no\"\u003eIMPERSONATED_EMAIL\u003c/var\u003e'\n\n### Perl\n\nSet the private key JSON path and delegate account ID in your\nconfiguration. If you're using a `googleads.properties` file, add the\nfollowing: \n\n jsonKeyFilePath=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n impersonatedEmail=\u003cvar translate=\"no\"\u003eIMPERSONATED_EMAIL\u003c/var\u003e\n\nIf you're using environment variables, add the following to your Bash\nconfiguration or environment: \n\n export GOOGLE_ADS_JSON_KEY_FILE_PATH=\u003cvar translate=\"no\"\u003eJSON_KEY_FILE_PATH\u003c/var\u003e\n export GOOGLE_ADS_IMPERSONATED_EMAIL=\u003cvar translate=\"no\"\u003eIMPERSONATED_EMAIL\u003c/var\u003e\n\n### Security concerns\n\nSince the service account has domain-level delegation control for your Google\nWorkspace domain, it's important to protect the key file that allows a service\naccount to access the Google services for which it's authorized. This is\nespecially true since that service account has the ability to impersonate any\nuser in the domain.\n\nAnother good practice is to allow service accounts to access only the minimum\nrequired set of APIs. This is a preemptive measure to limit the amount of data\nan attacker can access if the service account's key file is compromised."]]