Setting up Google Adwords OAuth is simple but can prove to be a bit tricky if the required steps aren't performed properly. This post assumes that the user has installed Composer on their machine.
 
Step 1 : The first step would be to setup a Laravel project. This can be done with the create-project command in the command line.
 
composer create-project laravel/laravel adwordsproject
 
Once the project is set up, the index page should show up on the browser.
 
 
Step 2: Now that the Laravel project is set up, we can proceed to including the Google Adwords library in our project. This can be done by
 
composer require googleads/googleads-php-lib
 
This will create generate a new folder in the vendor directory titled "googleads". This folder will contain the PHP library for Google Adwords.
 
Step 3: Setting up our app in the Google API Console
 
Next, we will set up our app in the Google API Console. Proceed to https://console.developers.google.com and log in. In the "Credentials" tab, Click "Create Credentials" and choose OAuth Client ID.
 
 
 
Choose the application type that you want to choose. For this walk through we will go with Web Application.
 
After filling in the name you will need to fill in the "Authorized redirect URIs" with your redirect URI (The page URL that the web application will redirect to after authorization). After this is done, click "Create". This will display a prompt which will display the Client ID and the Client Secret. Let's save this somewhere accessible.
 
 
Step 4 : Let's create a button, clicking on which will kickstart the authentication process.
 
<form action="{{ route('postPerformAuthentication') }} method="post">
<button type="submit">Connect a Google Adwards account</button>
<input type="hidden" value="{{ Session::token() }}" name="_token">
</form>
 
 
In the above code we are hooking up the form's action with a route named 'postPerformAuthentication'.
 
Step 5 : Now we will set up our route in the web.php file within the routes directory which will take care of the form POST request.
 
Route::post('/home',[
'uses' => 'GoogleAuthenticationController@postPerformAuthentication',
'as' => 'postPerformAuthentication'
]);
 
Step 6 : The next step is to implement the main functionality in the GoogleAuthenticationController. Initially the controller will look like this
 
class GoogleAuthenticationController extends Controller{
public
function postPerformAuthentication(Request $request)
{

}
}
 
 
Now, we will do the following things in our postPerformAuthentication function :
 
  1. First, create an OAuth2 instance. You will need to fill in the redirectUri, clientId, and clientSecret you created in Step 1 above, as well as the OAuth2 scope of the ads API you're using.
use Google\Auth\OAuth2;

session_start();

$oauth2 = new OAuth2([
    'authorizationUri' => 'https://accounts.google.com/o/oauth2/v2/auth',
    'tokenCredentialUri' => 'https://www.googleapis.com/oauth2/v4/token',
    'redirectUri' => '****',
    'clientId' => '****',
    'clientSecret' => '****',
    'scope' => '****'
]);
 
For our application we can set the scope to 'https://www.googleapis.com/auth/adwords'
 
2. Next, direct the user to a consent screen where they can authorize your app.
 
if (!isset($_GET['code'])) {
  // Create a 'state' token to prevent request forgery.
  // Store it in the session for later validation.
  $oauth2->setState(sha1(openssl_random_pseudo_bytes(1024)));
  $_SESSION['oauth2state'] = $oauth2->getState();

  // Redirect the user to the authorization URL.
  $config = [
    // Set to 'offline' if you require offline access.
    'access_type' => 'online'
  ];
  header('Location: ' . $oauth2->buildFullAuthorizationUri($config));
  exit;
}
 
3. Assuming you've set the redirectUri to come back to this same page, first validate the state, and then use the authorization code to get an access token (and a refresh token if you requested offline access).
// Check given state against previously stored one to mitigate CSRF attack.
 elseif (empty($_GET['state'])
    || ($_GET['state'] !== $_SESSION['oauth2state'])) {
  unset($_SESSION['oauth2state']);
  exit('Invalid state.');
} else {
  $oauth2->setCode($_GET['code']);
  $authToken = $oauth2->fetchAuthToken();

  // Store the refresh token for your user in your local storage if you
  // requested offline access.
  $refreshToken = $authToken['refresh_token'];
  ...
}

 

You are all set up now.

Step 7 : You can now use the OAuth2 object to make calls using the client library.
 
use Google\AdsApi\AdWords\AdWordsServices;
use Google\AdsApi\AdWords\AdWordsSessionBuilder;
use Google\AdsApi\Common\OAuth2TokenBuilder;

$session = (new AdWordsSessionBuilder())
    ->fromFile()
    ->withOAuth2Credential($oauth2)
    ->build();

$adWordsServices = new AdWordsServices();

$campaignService =
    $adWordsServices->get($session, 'CampaignService', 'v201603', 'cm');

// Make calls using $campaignService.