动态获取refreshToken
This commit is contained in:
parent
9da48235e3
commit
27b97ecc0a
@ -271,15 +271,25 @@ class GoogleAdsAccountService
|
||||
*/
|
||||
private static function createGoogleAdsClient(int $loginCustomerId)
|
||||
{
|
||||
// Generates a refreshable OAuth2 credential for authentication.
|
||||
$advertiserId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||||
|
||||
// 从数据库获取 access_token
|
||||
$refreshToken = self::getRefreshTokenFromDatabase($advertiserId);
|
||||
|
||||
if (!$refreshToken) {
|
||||
throw new \Exception("Access token not found for advertiserId: " . $advertiserId);
|
||||
}
|
||||
|
||||
// OAuth2 Token Authentication
|
||||
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||
// Sets the properties based on the default properties file
|
||||
->fromFile()
|
||||
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||
->build();
|
||||
|
||||
if($loginCustomerId >0){
|
||||
if ($loginCustomerId > 0) {
|
||||
// Builds and returns the Google Ads client
|
||||
return (new GoogleAdsClientBuilder())
|
||||
return (
|
||||
new GoogleAdsClientBuilder())
|
||||
// Sets the properties based on the default properties file
|
||||
->fromFile()
|
||||
// eUses the OAuth2 credentials crated above.
|
||||
@ -287,7 +297,7 @@ class GoogleAdsAccountService
|
||||
// Overrides the login customer ID with the given one.
|
||||
->withLoginCustomerId($loginCustomerId)
|
||||
->build();
|
||||
}else{
|
||||
} else {
|
||||
// Builds and returns the Google Ads client
|
||||
return (new GoogleAdsClientBuilder())
|
||||
// Sets the properties based on the default properties file
|
||||
@ -302,4 +312,20 @@ class GoogleAdsAccountService
|
||||
}
|
||||
|
||||
|
||||
// 从数据库动态获取 google RefreshToken
|
||||
private function getRefreshTokenFromDatabase($advertiserId)
|
||||
{
|
||||
// 使用 ThinkDb 进行联表查询
|
||||
// $advertiserId = 'your-advertiser-id'; // 假设你已经获得了广告商ID
|
||||
|
||||
$user = ThinkDb::table('bps_third_user_advertiser as a')
|
||||
->join('bps_third_user as u', 'a.doc_ = u.id', 'left') // 连接 bps_third_user 表
|
||||
->where('a.advertiser_id', $advertiserId)
|
||||
->select('u.access_token') // 只选择 access_token 字段
|
||||
->first();
|
||||
|
||||
return $user ? $user->access_token : null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -44,10 +44,20 @@ class GoogleAdsAdService
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// $this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||||
$advertiserId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||||
|
||||
// 从数据库获取 access_token
|
||||
$refreshToken = $this->getRefreshTokenFromDatabase($advertiserId);
|
||||
|
||||
if (!$refreshToken) {
|
||||
throw new \Exception("Access token not found for advertiserId: " . $advertiserId);
|
||||
}
|
||||
|
||||
// OAuth2 Token Authentication
|
||||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
|
||||
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||
->build();
|
||||
|
||||
// Google Ads Client initialization
|
||||
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
||||
@ -56,6 +66,20 @@ class GoogleAdsAdService
|
||||
->build();
|
||||
}
|
||||
|
||||
// 从数据库动态获取 google RefreshToken
|
||||
private function getRefreshTokenFromDatabase($advertiserId)
|
||||
{
|
||||
// 使用 ThinkDb 进行联表查询
|
||||
// $advertiserId = 'your-advertiser-id'; // 假设你已经获得了广告商ID
|
||||
|
||||
$user = ThinkDb::table('bps_third_user_advertiser as a')
|
||||
->join('bps_third_user as u', 'a.doc_ = u.id', 'left') // 连接 bps_third_user 表
|
||||
->where('a.advertiser_id', $advertiserId)
|
||||
->select('u.access_token') // 只选择 access_token 字段
|
||||
->first();
|
||||
|
||||
return $user ? $user->access_token : null;
|
||||
}
|
||||
|
||||
|
||||
/* @param int $customerId the customer ID
|
||||
@ -87,7 +111,7 @@ class GoogleAdsAdService
|
||||
{
|
||||
$tableName = 'bps_google_ads_ad';
|
||||
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'public' . $tableName;
|
||||
foreach ($groupadsResourceName as $data){
|
||||
foreach ($groupadsResourceName as $data) {
|
||||
$sql = "INSERT INTO {$tableName}
|
||||
(ad_id, ad_group_id, customer_id, ad_name, status, resource_name)
|
||||
VALUES (:ad_id, :ad_group_id, :customer_id, :ad_name, :status, :resource_name)
|
||||
|
@ -43,10 +43,20 @@ class GoogleAdsCampaignService
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// $this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||||
$advertiserId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||||
|
||||
// 从数据库获取 access_token
|
||||
$refreshToken = $this->getRefreshTokenFromDatabase($advertiserId);
|
||||
|
||||
if (!$refreshToken) {
|
||||
throw new \Exception("Access token not found for advertiserId: " . $advertiserId);
|
||||
}
|
||||
|
||||
// OAuth2 Token Authentication
|
||||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
|
||||
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||
->build();
|
||||
|
||||
// Google Ads Client initialization
|
||||
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
||||
@ -55,6 +65,21 @@ class GoogleAdsCampaignService
|
||||
->build();
|
||||
}
|
||||
|
||||
// 从数据库动态获取 google RefreshToken
|
||||
private function getRefreshTokenFromDatabase($advertiserId)
|
||||
{
|
||||
// 使用 ThinkDb 进行联表查询
|
||||
// $advertiserId = 'your-advertiser-id'; // 假设你已经获得了广告商ID
|
||||
|
||||
$user = ThinkDb::table('bps_third_user_advertiser as a')
|
||||
->join('bps_third_user as u', 'a.doc_ = u.id', 'left') // 连接 bps_third_user 表
|
||||
->where('a.advertiser_id', $advertiserId)
|
||||
->select('u.access_token') // 只选择 access_token 字段
|
||||
->first();
|
||||
|
||||
return $user ? $user->access_token : null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Runs the example.
|
||||
@ -340,7 +365,7 @@ class GoogleAdsCampaignService
|
||||
public static function saveCampaigns($campaignsResourceName)
|
||||
{
|
||||
$tableName = 'bps_google_ads_campaign';
|
||||
$tableName = getenv('DB_PG_SCHEMA')? getenv('DB_PG_SCHEMA').'.'.$tableName: 'public'.$tableName;
|
||||
$tableName = getenv('DB_PG_SCHEMA') ? getenv('DB_PG_SCHEMA') . '.' . $tableName : 'public' . $tableName;
|
||||
// foreach ($campaignsResourceName as $data) {
|
||||
// $sql = "INSERT INTO {$tableName}
|
||||
// (campaign_id, customer_id, campaign_name, status, advertising_channel_type, start_date, end_date, budget_amount_micros)
|
||||
|
@ -30,10 +30,20 @@ class GoogleAdsGroupService
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// $this->customerId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||||
$advertiserId = getenv('GOOGLE_ADS_CUSTOMER_ID');
|
||||
|
||||
// 从数据库获取 access_token
|
||||
$refreshToken = $this->getRefreshTokenFromDatabase($advertiserId);
|
||||
|
||||
if (!$refreshToken) {
|
||||
throw new \Exception("Access token not found for advertiserId: " . $advertiserId);
|
||||
}
|
||||
|
||||
// OAuth2 Token Authentication
|
||||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
|
||||
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||
->build();
|
||||
|
||||
// Google Ads Client initialization
|
||||
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
||||
@ -43,6 +53,22 @@ class GoogleAdsGroupService
|
||||
}
|
||||
|
||||
|
||||
// 从数据库动态获取 google RefreshToken
|
||||
private function getRefreshTokenFromDatabase($advertiserId)
|
||||
{
|
||||
// 使用 ThinkDb 进行联表查询
|
||||
// $advertiserId = 'your-advertiser-id'; // 假设你已经获得了广告商ID
|
||||
|
||||
$user = ThinkDb::table('bps_third_user_advertiser as a')
|
||||
->join('bps_third_user as u', 'a.doc_ = u.id', 'left') // 连接 bps_third_user 表
|
||||
->where('a.advertiser_id', $advertiserId)
|
||||
->select('u.access_token') // 只选择 access_token 字段
|
||||
->first();
|
||||
|
||||
return $user ? $user->access_token : null;
|
||||
}
|
||||
|
||||
|
||||
/* @param int $customerId the customer ID
|
||||
* @param $options
|
||||
* @return mixed
|
||||
|
@ -32,7 +32,7 @@ loginCustomerId = 1509096882
|
||||
; For installed application flow.
|
||||
clientId = "117429539543-t73vtg7v1vag5b2dg68qaaaj00gmacjs.apps.googleusercontent.com"
|
||||
clientSecret = "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"
|
||||
refreshToken = "1//0eNpv3UrnIRMaCgYIARAAGA4SNwF-L9Ir_W9Fs5CrdW_7IFjRkq2nA6TZwSyi9Y8ukj8nirWt3BvOR74j3HatYX3cug7vfzGPUhs"
|
||||
;refreshToken = "1//0eNpv3UrnIRMaCgYIARAAGA4SNwF-L9Ir_W9Fs5CrdW_7IFjRkq2nA6TZwSyi9Y8ukj8nirWt3BvOR74j3HatYX3cug7vfzGPUhs"
|
||||
|
||||
; For service account flow.
|
||||
; jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE"
|
||||
|
Loading…
Reference in New Issue
Block a user