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 acomposer.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 asgoogle_ads_auth.php
, and write OAuth authentication in it Code:
'', 'clientSecret' => ' ', 'redirectUri' => ' ', ]); $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,
,
and
need to be replaced with actual value.
and
are the information of the OAuth client ID created in the Google Ads developer console,
is the The redirect URI set in the console.
By accessing thegoogle_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 acode
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!