How to Integrate Google Adwords API in Laravel App? https://www.bacancytechnology.com/
Introduction
Have you encountered a requirement where you need to implement Google Ads API? If yes, and wondering how can you do it then here is a blog for you! A few weeks back, one of our developers had a requirement for Google Adwords API integration in the laravel application. And searching for a proper tutorial was a real struggle! That’s when it struck us to come up with a tutorial for fellow Laravel developers. Without further ado, let’s move ahead and see how to integrate Google Adwords API in Laravel App.
Required Package
We will be needing the following packages to integrate Google Adwords API in Laravel App.
googleads/google-ads-php PHP client library for the Google Adwords API.
ext-bcmath BCMath is a PHP extension helping us with float values. BC is known for Binary Calculator in BCMath.
ext-grpc The framework gRPC will be used for developing scalable APIs. It will lessen our struggle and allow transparent clientserver communication and connected systems.
ext-protobuf The protobuf implements Protocol Buffer of Google for PHP language that supports serialization of binary data and includes a protoc plugun for generating classes from .proto files.
Update composer.json with the following packages composer require googleads/google-adsphp composer require ext-bcmath composer require ext-grpc composer require ext-protobuf
Required PHP Extension
Make sure you have the following PHP modules in your system to integrate Google Adwords API. Bcmatch Grpc Protobuf Soap
Stop searching for the best Laravel developers. Because here we are! Bacancy will provide you proficient and dedicated developers with fundamental and advanced Laravel knowledge. Contact us to hire Laravel developer today!
Generate client ID and client secret key
Follow these instructions to generate a client ID and client secret key.
Open the Google API Console Credentials page. Navigate the following path to create a project: Select a project > NEW PROJECT > Enter project’s name OR Edit the provided project ID. Click Create. Select Create credentials and click on OAuth client ID on the Credentials page. If prompted for setting the product name on the Consent screen, click Configure consent screen, provide the information asked for, and click Save and you will return to the Credentials screen.
Select the Application type and click Create Copy your client ID and client secret as you will be needing them for configuring your client library.
Enable Google Ads API
For using the Google Ads API we will need to enable it. So, follow these instructions to enable the Google Adwords API. Open the Google API Console Credentials page Navigate to APIs & Services -> Library Search for Google Ads API Open the Google Ads API library and enable it
Generate Developer Token
Without a developer token, you won’t be able to proceed further. With the help of the below steps generate your developer token. Login to Google Ads Manager account Navigate to TOOLS & SETTINGS > SETUP > API Center. The API Center option appears only for Google Ads manager accounts. Fill out the form to request a developer token. Note: Only approved developer token will be used for production. A non-approved developer token can only be used with a test account.
Generate Refresh Token
Run AuthenticateInDesktopApplication.php or AuthenticateInWebApplication.php in the terminal. To run any of the above files go to path for file then php filename.
Fetch Data using Google Adwords API in Laravel App
So, far we are done with the initial project set up and generating a developer token for using Google Ads API. Now, let’s do some coding. Copy sample google_ads_php.ini to home directory. Set google_ads_php.ini path in app config.
'google_ads_php_path' => realpath(base_path('google_ads_php.ini'))
Register GoogleAdsClient in AppServiceProvider use Google\Ads\GoogleAds\Lib\V9\GoogleAdsCl ientBuilder; use Google\Ads\GoogleAds\Lib\OAuth2TokenB uilder; public function register() { // Binds the Google Ads API client. $this->app>singleton('Google\Ads\GoogleAds\Lib\V9\ GoogleAdsClient', function () { // Constructs a Google Ads API client configured from the properties file. return (new GoogleAdsClientBuilder()) >fromFile(config('app.google_ads_php_pat h')) ->withOAuth2Credential((new OAuth2TokenBuilder()) >fromFile(config('app.google_ads_php_pat h')) ->build()) ->build(); }); }
Github Repository: Google Ads API Integration Demo
You can visit the source code: googleads-api-demo and play around with the code.
Conclusion I hope the tutorial on integrating Google Adwords API in Laravel App was helpful to you. If you have any questions or suggestions feel free to contact us. For more such tutorials, visit Laravel tutorials page where you can clone the repository, start exploring the code, and learn about more Laravel.
Thank You
https://www.bacancytechnology.com/