Để 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 sẽ hướng dẫn bạn cách thiết lập OAuth 2.0 để truy cập API bằng thông tin đăng nhập của riêng bạn và quy trình trên máy tính hoặc quy trình trên web. Bạn chỉ cần thực hiện các bước này một lần, trừ phi bạn thu hồi, xoá hoặc cần thay đổi các phạm vi được phép cho thông tin đăng nhập OAuth 2.0.
Lưu lại mã ứng dụng khách và khoá bí mật của ứng dụng khách, sau đó quay lại trang này.
Thiết lập thư viện ứng dụng
Trong một thiết bị đầu cuối, hãy chạy ví dụ GenerateUserCredentials. Nhập mã ứng dụng khách OAuth 2.0 và khoá bí mật của ứng dụng khách khi được nhắc.
dotnet run GenerateUserCredentials.csproj
Ví dụ về mã này sẽ tự động mở một URL trong trình duyệt. Nếu bạn chưa đăng nhập vào phiên trình duyệt, trang web sẽ nhắc bạn đăng nhập vào Tài khoản Google. Nếu bạn đã đăng nhập, hệ thống sẽ bỏ qua lời nhắc đăng nhập và chuyển hướng bạn đến một URL nơi bạn phải uỷ quyền cho ứng dụng truy cập vào tài khoản Google Ads thay cho bạn.
Đăng nhập bằng Tài khoản Google mà bạn dùng để truy cập vào Google Ads. Thông thường, đây là email đăng nhập vào một tài khoản người quản lý Google Ads chứa tất cả các tài khoản mà bạn cần quản lý trong hệ thống phân cấp tài khoản của tài khoản người quản lý đó. Nhấp vào Tiếp tục trên màn hình đồng ý OAuth 2.0.
Bạn sẽ được chuyển đến một trang có thông báo cho biết rằng quá trình uỷ quyền đã thành công.
Your refresh token has been fetched. Check the
console output for further instructions.
Quay lại bảng điều khiển nơi bạn đang chạy ví dụ về mã. Bạn sẽ thấy ví dụ về mã đã hoàn tất và đang hiển thị mã làm mới cũng như một số hướng dẫn, sau đó là hướng dẫn mà bạn cần làm theo để định cấu hình thư viện ứng dụng:
Copy the following content into your App.config file.
<add key = 'OAuth2Mode' value = 'APPLICATION' />
<add key = 'OAuth2ClientId' value = '******' />
<add key = 'OAuth2ClientSecret' value = '******' />
<add key = 'OAuth2RefreshToken' value = '******' />
Đảm bảo rằng bạn cũng đặt tiêu đề LOGIN_CUSTOMER_ID trong tệp App.config. Hãy xem
hướng dẫn định cấu hình để tìm hiểu thêm.
<add key = 'LoginCustomerId' value = 'INSERT_LOGIN_CUSTOMER_ID_HERE' />
Tạo thông tin xác thực OAuth2 cho các ứng dụng tương tác
Nếu không biết trước tài khoản Google Ads để thực hiện các lệnh gọi API, bạn có thể khám phá tài khoản đó trong thời gian chạy bằng cách sử dụng thông tin đăng nhập của người dùng đã đăng nhập cho phiên. Hãy xem AuthenticateInAspNetCoreApplication để biết ví dụ về trường hợp người dùng hiện đang đăng nhập được xác thực và thông tin xác thực OAuth2 được lấy trong thời gian chạy.
[null,null,["Cập nhật lần gần đây nhất: 2025-08-31 UTC."],[[["\u003cp\u003eStarting with version 19.0.0, the Google Ads API client library for .NET will no longer support .NET 5.0, requiring an upgrade before the end of 2024 for continued functionality.\u003c/p\u003e\n"],["\u003cp\u003eThis guide explains how to set up OAuth2 for API access using your own credentials, offering two methods: the desktop flow and the web flow.\u003c/p\u003e\n"],["\u003cp\u003eYou can simplify API calls by linking your Google Ads accounts under a single manager account and using the provided example to generate OAuth2 credentials.\u003c/p\u003e\n"],["\u003cp\u003eFor interactive applications where the Google Ads account is unknown beforehand, you can discover it at runtime using the credentials of the signed-in user, as demonstrated in the provided example.\u003c/p\u003e\n"]]],[],null,["# OAuth Desktop and Web Application Flows\n\nThis guide walks you through how to set up OAuth 2.0 for API access using your\nown credentials and either the [desktop flow](/identity/protocols/oauth2/native-app)\nor the [web flow](/identity/protocols/oauth2/web-server). These steps only need\nto be done once, unless you revoke, delete, or need to change the allowed scopes\nfor your OAuth 2.0 credentials.\n\nCreate OAuth 2.0 credentials\n----------------------------\n\n1. Follow the steps to [configure a Google API Console project for the\n Google Ads API](/google-ads/api/docs/oauth/cloud-project).\n\n2. Note the *client ID* and *client secret*, then come back to this page.\n\nSet up the client library\n-------------------------\n\n| The following steps run an interactive code example, which will require you to provide input.\n\n1. In a terminal, run the [`GenerateUserCredentials`](https://github.com/googleads/google-ads-dotnet/blob/main/Google.Ads.GoogleAds/examples/Authentication/GenerateUserCredentials)example. Enter the OAuth 2.0 client ID and client secret\n when prompted.\n\n ```\n dotnet run GenerateUserCredentials.csproj\n ```\n2. This code example opens a URL automatically in the browser. If you aren't signed in to your\n browser session, the web page will prompt you to sign in to your Google Account. If you are\n already signed in, it will skip the sign-in prompt and redirect you to a URL where you must\n authorize the app to access your Google Ads account on your behalf.\n\n Sign in with the Google Account you use to access Google Ads. Usually, this is a login\n email to a Google Ads manager account that contains all the accounts you need to manage\n under its account hierarchy. Click **Continue** on the OAuth 2.0 consent screen.\n\n You'll be taken to a page with a message indicating that the authorization\n succeeded. \n\n ```\n Your refresh token has been fetched. Check the\n console output for further instructions.\n ```\n3. Return to the console where you're running the code example. You'll see that the code\n example has completed and is displaying your refresh token and some instructions, followed by\n the instructions you'll need to follow to configure the client library:\n\n Copy the following content into your App.config file.\n\n \u003cadd key = 'OAuth2Mode' value = 'APPLICATION' /\u003e\n \u003cadd key = 'OAuth2ClientId' value = '******' /\u003e\n \u003cadd key = 'OAuth2ClientSecret' value = '******' /\u003e\n \u003cadd key = 'OAuth2RefreshToken' value = '******' /\u003e\n\n Make sure you also set the `LOGIN_CUSTOMER_ID` header in your `App.config`\n file. See [the configuration guide](/google-ads/api/docs/client-libs/dotnet/configuration#settings) to learn more. \n\n ```\n \u003cadd key = 'LoginCustomerId' value = 'INSERT_LOGIN_CUSTOMER_ID_HERE' /\u003e\n ```\n\n Refer to the [configuration guide](/google-ads/api/docs/client-libs/dotnet/configuration) for\n alternate configuration options.\n\nGenerate OAuth2 credentials for interactive applications\n--------------------------------------------------------\n\nIf you don't know the Google Ads account to make API calls to in advance, you can\ndiscover it at runtime using the credentials of the signed in user for the\nsession. See\n[`AuthenticateInAspNetCoreApplication`](https://github.com/googleads/google-ads-dotnet/tree/HEAD/Google.Ads.GoogleAds/examples/Authentication/AuthenticateInAspNetCoreApplication)\nfor an example where the currently signed in user is authenticated and the OAuth2\ncredentials are obtained at runtime."]]