动态获取refreshToken
This commit is contained in:
parent
9da48235e3
commit
27b97ecc0a
@ -271,15 +271,25 @@ class GoogleAdsAccountService
|
|||||||
*/
|
*/
|
||||||
private static function createGoogleAdsClient(int $loginCustomerId)
|
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())
|
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||||
// Sets the properties based on the default properties file
|
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||||
->fromFile()
|
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||||
->build();
|
->build();
|
||||||
|
|
||||||
if ($loginCustomerId > 0) {
|
if ($loginCustomerId > 0) {
|
||||||
// Builds and returns the Google Ads client
|
// Builds and returns the Google Ads client
|
||||||
return (new GoogleAdsClientBuilder())
|
return (
|
||||||
|
new GoogleAdsClientBuilder())
|
||||||
// Sets the properties based on the default properties file
|
// Sets the properties based on the default properties file
|
||||||
->fromFile()
|
->fromFile()
|
||||||
// eUses the OAuth2 credentials crated above.
|
// eUses the OAuth2 credentials crated above.
|
||||||
@ -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()
|
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
|
// OAuth2 Token Authentication
|
||||||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
|
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||||
|
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||||
|
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||||
|
->build();
|
||||||
|
|
||||||
// Google Ads Client initialization
|
// Google Ads Client initialization
|
||||||
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
||||||
@ -56,6 +66,20 @@ class GoogleAdsAdService
|
|||||||
->build();
|
->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
|
/* @param int $customerId the customer ID
|
||||||
|
@ -43,10 +43,20 @@ class GoogleAdsCampaignService
|
|||||||
|
|
||||||
public function __construct()
|
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
|
// OAuth2 Token Authentication
|
||||||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
|
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||||
|
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||||
|
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||||
|
->build();
|
||||||
|
|
||||||
// Google Ads Client initialization
|
// Google Ads Client initialization
|
||||||
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
||||||
@ -55,6 +65,21 @@ class GoogleAdsCampaignService
|
|||||||
->build();
|
->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.
|
* Runs the example.
|
||||||
|
@ -30,10 +30,20 @@ class GoogleAdsGroupService
|
|||||||
|
|
||||||
public function __construct()
|
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
|
// OAuth2 Token Authentication
|
||||||
$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
|
$oAuth2Credential = (new OAuth2TokenBuilder())
|
||||||
|
->fromFile() // 如果需要从文件获取其他配置,可以继续使用 fromFile()
|
||||||
|
->withRefreshToken($refreshToken) // 使用动态获取的 access_token
|
||||||
|
->build();
|
||||||
|
|
||||||
// Google Ads Client initialization
|
// Google Ads Client initialization
|
||||||
$this->googleAdsClient = (new GoogleAdsClientBuilder())
|
$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 int $customerId the customer ID
|
||||||
* @param $options
|
* @param $options
|
||||||
* @return mixed
|
* @return mixed
|
||||||
|
@ -32,7 +32,7 @@ loginCustomerId = 1509096882
|
|||||||
; For installed application flow.
|
; For installed application flow.
|
||||||
clientId = "117429539543-t73vtg7v1vag5b2dg68qaaaj00gmacjs.apps.googleusercontent.com"
|
clientId = "117429539543-t73vtg7v1vag5b2dg68qaaaj00gmacjs.apps.googleusercontent.com"
|
||||||
clientSecret = "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"
|
clientSecret = "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"
|
||||||
refreshToken = "1//0eNpv3UrnIRMaCgYIARAAGA4SNwF-L9Ir_W9Fs5CrdW_7IFjRkq2nA6TZwSyi9Y8ukj8nirWt3BvOR74j3HatYX3cug7vfzGPUhs"
|
;refreshToken = "1//0eNpv3UrnIRMaCgYIARAAGA4SNwF-L9Ir_W9Fs5CrdW_7IFjRkq2nA6TZwSyi9Y8ukj8nirWt3BvOR74j3HatYX3cug7vfzGPUhs"
|
||||||
|
|
||||||
; For service account flow.
|
; For service account flow.
|
||||||
; jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE"
|
; jsonKeyFilePath = "INSERT_ABSOLUTE_PATH_TO_OAUTH2_JSON_KEY_FILE_HERE"
|
||||||
|
Loading…
Reference in New Issue
Block a user