Generate OAuth2 credentials for a single account
The easiest way to make calls to Google Ads API accounts is to
Link all the accounts you want to make API calls to under a single Google Ads manager account.
GenerateUserCredentialsexample to generate OAuth2 credentials for a user with access to the manager account.
Use the OAuth2 credentials to make calls to all the accounts you need to manage using Google Ads API.
These steps only need to be done once, unless you revoke, delete, or need to change the allowed scopes for your OAuth2 credentials.
Step 1 - Create OAuth2 credentials
Follow the steps to configure a Google API Console project for the Google Ads API.
Note the client ID and secret, then come back to this page.
Step 2 - Set up the client library
In a terminal, run the
GenerateUserCredentialsexample, which prompts you for your OAuth2 client ID and secret.
This is an interactive example, which will require you to provide input.
dotnet run GenerateUserCredentials.csproj
The example prompts you to provide the OAuth2 client ID and secret. Provide the details from step 1.
The example opens a URL automatically in the browser. If you aren't signed in to your browser session, the web page will prompt you to sign in to the Google Account. If you are already signed in, it will skip the sign in prompt and redirect you to a URL where you are prompted to authorize the application to access your Google Ads account on your behalf.
You are then taken to a page with a message indicating that the authorization succeeded.
Your refresh token has been fetched. Check the console output for further instructions.
Return to the console where you were running the example. The example should have completed and be displaying your refresh token and some instructions, followed by the properties you'll need to configure the client library:
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 = '******' /> <!-- Required for manager accounts only: Specify the login customer --> <!-- ID used to authenticate API calls. This will be the customer ID --> <!-- of the authenticated manager account. It should be set without --> <!-- dashes, for example: 1234567890 instead of 123-456-7890. You can --> <!-- also specify this later in code if your application uses --> <!-- multiple manager account OAuth pairs. --> <add key = 'LoginCustomerId' value = INSERT_LOGIN_CUSTOMER_ID_HERE /> See developers.google.com/google-ads/api/docs/client-libs/dotnet/configuration for alternate configuration options.
Generate OAuth2 credentials for interactive applications
If you don't know the Google Ads account to make API calls to in advance, you can
discover it at runtime using the credentials of the signed in user for the
for an example where the currently signed in user is authenticated and the OAuth2
credentials are obtained at runtime.