Working With Google APIs OAuth2 In C#

Programming & Tech

Working With Google APIs OAuth2 In C#

You probably wanted to work with google client API once and got very lost because you didn’t know how to get OAuth2 to work properly.

In this blog post I’m going to take you through the steps you have to take to get OAuth2 Working flawlessly in your application.

In my case I’m working on an API that uses Ad words key planner to generate Keywords from a URL and/or a keyword.

Google Developer Console:


Go to, create a project or select an existing one if you already created it and used other google APIs.

Click on create credentials

Select OAuth Client ID

Select the application type, in my case it is an API that I’m developing so i will select web application.

Then Fill out the rest of the information and add the following URL to the list of Redirect URLs :

When you click on create a popup will show you the following details :

Source : Google Documentation

Now save these to a file for use later.

Service APIs

Now We Need to activate the service we want to work with. in my case it is Ad-words so i will activate the Ads API. Look for the service you want to use in your app and activate it, it will give you the access to that service and the ability to generate API keys for the specific service.

Search For The Service you want to activate

After Activating it will look something like this :

For API keys click on Manage > Credentials > Get API Key

Testing Project And Testing Accounts

Unless you are actually in a production environment i recommend you make the project into a testing a project. this will also help us to get our refresh token.

Look For publishing status, and click on back to testing

Now go to Test users and add a google account you have logged in in this browser or a google account that you want to test with ( current google account for example)

Now we have finished this step we go to the next one.

Getting the Refresh Token

Access tokens are temporary but refresh token can get you new ones each time.

We will get them from the OAuth2 Playground

go to OAuth Playground and fill out the information that you have saved previously :

Source : Google Documentation

Now On the left select the API you want to authenticate, and click on authenticate :

Source : Google Documentation

You will get an authorization code as a result but we don’t need that, what we need is a refresh token and an access token.

We Click on exchange then we will get them .

Once you are here you have all the required credentials.

If you need a developer token then we jump to our next Step

Getting the developer token ( AdWords )

First create a Production Manager Account : link

Then Request a developer token by going to Tools And Settings > API Center and then requesting a developer token

Then Create a test manager account, this will give you the customer ID to test you APIs, but if you are in a production environment and the API Key is approved you use the Customer ID for the main account.

Though when you request a key it won’t be approved instantly so i recommend you use a test account meanwhile.


With this last step done you have all you need to create you final config file.

in your main project folder create an app.config file and put the following :

<?xml version="1.0" encoding="utf-8" ?>
    <section name="GoogleAdsApi"
    <!-- Settings specific to Google Ads API.-->
    <add key="DeveloperToken" value="xxxxxxxxxxxxxxxxxxxxxx"/>

    <!-- OAuth2 configuration -->
    <add key="AuthorizationMethod" value="OAuth2" />
    <add key="OAuth2ClientId" value="" />
    <add key="OAuth2ClientSecret" value="xxxxxxxxxxxxxxxx" />
    <add key="OAuth2Mode" value="APPLICATION" />
    <add key="OAuth2RefreshToken" value="xxxxxxxxxxxxxxxxxxxxx" />

You replace the values with your own and there you go it is ready for use.

Tags: , ,

Leave a Reply