Para analizar nuestros productos y brindar comentarios sobre ellos, únete al canal oficial de Discord de Google Ads en el servidor de la Comunidad de Publicidad y Medición de Google.
Flujos de aplicaciones web y de escritorio de OAuth
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
En esta guía, se explica cómo configurar OAuth 2.0 para el acceso a la API con tus propias credenciales y el flujo de escritorio o el flujo web. Solo debes realizar estos pasos una vez, a menos que revoques, borres o necesites cambiar los alcances permitidos para tus credenciales de OAuth 2.0.
Anota el ID de cliente y el secreto del cliente y, luego, vuelve a esta página.
Configura la biblioteca cliente
En una terminal, ejecuta el ejemplo de GenerateUserCredentials. Cuando se te solicite, ingresa el ID y el secreto del cliente de OAuth 2.0.
dotnet run GenerateUserCredentials.csproj
En este ejemplo de código, se abre una URL automáticamente en el navegador. Si no accediste a tu sesión del navegador, la página web te solicitará que accedas a tu Cuenta de Google. Si ya accediste, se omitirá el mensaje de acceso y se te redireccionará a una URL en la que deberás autorizar a la app para que acceda a tu cuenta de Google Ads en tu nombre.
Accede con la Cuenta de Google que usas para ingresar a Google Ads. Por lo general, se trata de un correo electrónico de acceso a una cuenta de administrador de Google Ads que contiene todas las cuentas que necesitas administrar en su jerarquía de cuentas. Haz clic en Continuar en la pantalla de consentimiento de OAuth 2.0.
Se te redireccionará a una página con un mensaje que indica que la autorización se realizó correctamente.
Your refresh token has been fetched. Check the
console output for further instructions.
Regresa a la consola en la que ejecutas el ejemplo de código. Verás que el ejemplo de código se completó y muestra tu token de actualización y algunas instrucciones, seguidas de las instrucciones que deberás seguir para configurar la biblioteca cliente:
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 = '******' />
Asegúrate de configurar también el encabezado LOGIN_CUSTOMER_ID en tu archivo App.config. Consulta
la guía de configuración para obtener más información.
<add key = 'LoginCustomerId' value = 'INSERT_LOGIN_CUSTOMER_ID_HERE' />
Genera credenciales de OAuth2 para aplicaciones interactivas
Si no conoces de antemano la cuenta de Google Ads a la que debes realizar llamadas a la API, puedes descubrirla en el tiempo de ejecución con las credenciales del usuario que accedió a la sesión. Consulta AuthenticateInAspNetCoreApplication para ver un ejemplo en el que el usuario que accedió actualmente está autenticado y las credenciales de OAuth2 se obtienen en el tiempo de ejecución.
[null,null,["Última actualización: 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."]]