Home > Article > Backend Development > How to authenticate with Google Ads using PHP and OAuth
How to use PHP and OAuth for Google Ads authentication
In today's digital advertising field, Google Ads is a very commonly used platform for advertisers. In order to use Google Ads services on our website or application, we first need to authenticate. Using PHP and OAuth for Google Ads authentication is a common method.
OAuth is an open standard authorization protocol that allows users to let third-party applications access their personal information stored on a service provider without providing an account and password on the service provider. Google Ads also supports the OAuth authorization mechanism, so we can use applications written in PHP for authentication.
Here are the steps and code examples for Google Ads authentication using PHP and OAuth:
First, we need Create an API key in the Google Ads Developer Console. Once logged into the console, select your project or create a new project and create a new "OAuth Client ID" in the Credentials menu.
We need to install the OAuth library in the PHP project, which can be managed through Composer.
Create a composer.json
file in the root directory of the project and add the following content:
{ "require": { "league/oauth2-client": "^2.6" } }
Then switch to the project root directory in the terminal and execute The following command installs the OAuth library:
$ composer install
Create a PHP file, such as google_ads_auth.php
, and write OAuth authentication in it Code:
<?php require 'vendor/autoload.php'; $client = new LeagueOAuth2ClientProviderGoogle([ 'clientId' => '<YOUR_CLIENT_ID>', 'clientSecret' => '<YOUR_CLIENT_SECRET>', 'redirectUri' => '<YOUR_REDIRECT_URI>', ]); $authUrl = $client->getAuthorizationUrl(['scope' => 'https://www.googleapis.com/auth/adwords']); if (!isset($_GET['code'])) { // 如果不包含oauth2授权代码,则重定向到Google登录页面 header('Location: ' . $authUrl); exit; } else { // 如果包含oauth2授权代码,则从Google获取访问令牌 $accessToken = $client->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); $refreshToken = $accessToken->getRefreshToken(); $expires = $accessToken->getExpires(); // 使用访问令牌进行Google Ads API调用 // ... // 保存访问令牌和刷新令牌,以便后续使用 // ... }
In the above code, 761318c14e3a4c03d706f9654038059f
, 78c9c9a369e6b9dcaa032e7ffdf4c7f8
and 07de7cb4a42d8009d3825fdc0b88f253
need to be replaced with actual value. 761318c14e3a4c03d706f9654038059f
and 78c9c9a369e6b9dcaa032e7ffdf4c7f8
are the information of the OAuth client ID created in the Google Ads developer console, 07de7cb4a42d8009d3825fdc0b88f253
is the The redirect URI set in the console.
By accessing the google_ads_auth.php
file, you will be redirected without an access token Directed to the Google login page, enter your Google Ads account to log in. After successfully logging in, you will receive a code
parameter value as the query parameter in the callback URL.
After obtaining the access token code, you can use the access token to call the Google Ads API. According to the Google Ads documentation, you can use the corresponding API libraries and methods to implement specific functions.
Summary:
Through the above steps, we can use PHP and OAuth for Google Ads authentication. After mastering these basic knowledge, we can use the Google Ads API in our website or application to implement more complex functions, such as creating ad campaigns, managing ad groups, etc.
The above is the detailed content of How to authenticate with Google Ads using PHP and OAuth. For more information, please follow other related articles on the PHP Chinese website!