Skip to content

API access using own credentials (installed application flow)

Chris Seeley edited this page Jun 11, 2019 · 3 revisions

This guide will walk you through how to setup OAuth2 for API access using your own credentials using installed application flow. 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 - Creating OAuth2 credentials

Follow the steps for the product you're using to generate a client ID and secret, then come back to this page.

Step 2 - Setting up the client library

Adding OAuth2 support for your application (single login)

If your application manages only one Advertiser account (or a hierarchy of Advertiser accounts all linked under a single master MCC), then you don’t need to build OAuth2 flow into your application. You can instead use a utility named OAuthTokenGenerator.exe to generate the necessary OAuth2 configuration. Refer to this wiki article for generating OAuth2 configuration using OAuthTokenGenerator.

Adding OAuth2 support for your application (multiple logins)

If you manage multiple unrelated AdWords accounts, then you need to build OAuth2 sign-in flow into your application as part of adding OAuth2 support for your application.

  1. Configure the following keys in your App.config / Web.config.
<add key="AuthorizationMethod" value="OAuth2" />
<add key="OAuth2ClientId" value="INSERT_OAUTH2_CLIENT_ID_HERE" />
<add key="OAuth2ClientSecret" value="INSERT_OAUTH2_CLIENT_SECRET_HERE" />
<add key="OAuth2Mode" value="APPLICATION" />
  1. Prompt your users to open the Authorization Url in a web browser, authorize the application and provide the authorizationCode. The following code shows how this is done for a console application:
public class ConsoleExample {
  static void Main(string[] args) {
    AdWordsUser user = new AdWordsUser();
    DoAuth2Authorization(user);
     
    // Make your API calls.
  }

  private static void DoAuth2Authorization(AdWordsUser user) {
    AdsOAuthProviderForApplications oAuth2Provider =
        (user.OAuthProvider as AdsOAuthProviderForApplications);
    // Get the authorization url.
    string authorizationUrl = oAuth2Provider.GetAuthorizationUrl();
    Console.WriteLine("Open a fresh web browser and navigate to \n\n{0}\n\n. You will be " +
        "prompted to login and then authorize this application to make calls to the " +
        "AdWords API. Once approved, you will be presented with an authorization code.",
        authorizationUrl);

    // Accept the OAuth2 authorization code from the user.
    Console.Write("Enter the authorization code :");
    string authorizationCode = Console.ReadLine();

    // Fetch the access and refresh tokens.
    oAuth2Provider.FetchAccessAndRefreshTokens(authorizationCode);
  }
}

If you don’t want your users to leave your application, you could embed a WebBrowser in your application to capture the OAuth2 authorizationCode. You can refer to the source code for OAuthTokenGenerator application for a code example.