zeek / laravel-googleads
简化了在Laravel中使用google-ads-php客户端库和Google Ads查询语言。
Requires
- googleads/google-ads-php: ^22.1
- illuminate/support: ^8.0 || ^9.0 || ^10.0
Requires (Dev)
- mockery/mockery: ^1.4.4
- nesbot/carbon: ^2.66
- orchestra/testbench: ^7.0 || ^8.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-08-30 19:58:06 UTC
README
简化了在Laravel中使用google-ads-php客户端库和Google Ads查询语言
安装
composer require zeek/laravel-googleads
配置
php artisan vendor:publish --provider="Zeek\GoogleAds\GoogleAdsServiceProvider"
修改 config/googleads.php
文件以包含您的Google Ads凭据和其他设置。您需要以下内容
developerToken
- 您的Google Ads开发者令牌,请参阅 https://developers.google.com/google-ads/api/docs/get-started/dev-token & https://developers.google.com/google-ads/api/docs/best-practices/test-accountsclientId
&clientSecret
- 您的Google API Console OAuth2客户端ID & 密钥,请参阅 https://developers.google.com/google-ads/api/docs/get-started/oauth-cloud-projectrefreshToken
- 访问账户的刷新令牌,请参阅下面的使用示例了解如何使用Socialite获取此令牌。clientCustomerId
- 您想要访问的账户的客户ID,请参阅 https://developers.google.com/google-ads/api/docs/best-practices/test-accounts
警告:开发者令牌将在首次使用后永久关联到OAuth2客户端ID。如果您需要更改开发者令牌,您将需要创建一个新的Google Cloud项目 & 新的OAuth2凭据。
注意:请确保您的Google Cloud项目已启用Google Ads API。
备注
Google Ads没有只读作用域
请参阅 https://groups.google.com/g/adwords-api/c/B0VVHqNOLYs,唯一可用的oauth作用域是 https://www.googleapis.com/auth/adwords
,它是读写。为了防止意外写入,您应该为只读访问使用单独的Google Ads账户。
使用
获取Google Ads客户端
$googleAds = App::make('google-ads');
或
use Zeek\GoogleAds\GoogleAds; $googleAds = new GoogleAds([ 'developerToken' => env('GOOGLE_ADS_DEVELOPER_TOKEN'), 'clientCustomerId' => env('GOOGLE_ADS_CLIENT_CUSTOMER_ID'), 'loginCustomerId' => env('GOOGLE_ADS_LOGIN_CUSTOMER_ID'), 'clientId' => env('GOOGLE_ADS_CLIENT_ID'), 'clientSecret' => env('GOOGLE_ADS_CLIENT_SECRET'), 'refreshToken' => env('GOOGLE_ADS_REFRESH_TOKEN'), ]);
列出已认证用户可访问的所有客户
$ads = new GoogleAds([ 'clientId' => config('services.google.client_id'), 'clientSecret' => config('services.google.client_secret'), 'developerToken' => config('services.google.ads_dev_token'), 'refreshToken' => $user->google_refresh_token, ]); $ads->listCustomers(); // ['customerId' => 'Descriptive Name']
Google Ads查询语言
注意:此功能尚未实现。
$googleAds = App::make('google-ads'); $googleAds->query('SELECT campaign.id, campaign.name FROM campaign');
使用Socialite获取刷新令牌
Laravel Socialite 使得与OAuth提供者协作变得容易。以下是如何使用它为Google Ads账户获取刷新令牌的示例
注意:请确保将您的应用程序主机名添加到Google API控制台中的授权JavaScript来源,并将重定向URI添加到授权重定向URI中。(例如,
https://example.com
和https://example.com/oauth-redirect
)
routes/web.php
:
Route::get('google-auth', [GoogleController::class, 'redirectToGoogle']) ->name('google-auth.init'); Route::get('oauth-redirect', [GoogleController::class, 'handleGoogleCallback']);
App\Http\Controllers\GoogleController.php
:
注意:
Record
是一个模型,您将在其中存储您的刷新令牌。
class GoogleController extends Controller { /** * Create a new controller instance. */ public function redirectToGoogle(Request $request, Record $record) { $driver = Socialite::driver('google'); $driver->scopes($this->getScopes()) ->with([ 'access_type' => 'offline', 'prompt' => 'consent select_account', ]); Session::put('record_id', $record->id); Session::forget('google-connection-error'); return $driver->redirect(); } public function handleGoogleCallback(Request $request) { $record = Record::find(Session::get('record_id')); try { $driver = Socialite::driver('google'); $oauth = $driver->user(); // Update record info with oauth provider's info $campaign->update([ 'google_id' => $oauth->id, 'google_refresh_token' => $oauth->refreshToken, ]); } catch (ClientException $e) { } Session::forget('google-connection-error'); return redirect(route('records.edit', $record))->with('status', 'Select your Google Ads customer account.'); } private function getScopes(): array { return [ 'https://www.googleapis.com/auth/adwords', ]; } }