如何将 Google 服务 oAuth2 转变为 Google 广告 API oAuth2 访问
How do you turn a Google Services oAuth2 into a Google Ads API oAuth2 access
所以我正在为 PHP 使用 Google API 客户端并且我有一个有效的 OAuth 流程,
class GoogleClient {
private static $client_id = "1050479587066-f64vq210hc2m15fdj4r77g8ml7jin30d.apps.googleusercontent.com";
private static $client_Secret = "CK8orQfPNpD9UgF0bqNJinVI";
private static $redirect_uri = '/return.php';
private static $access;
private static $client = null;
private static function checkForAccess(){
if(isset(self::$access)){
return true;
}
if(isset($_SESSION['GoogleAuth'])){
self::$access = $_SESSION['GoogleAuth'];
return true;
}
return false;
}
public static function GetClient(){
if(is_null(self::$client)){
$params = [
"client_id" => self::$client_id,
"client_secret" => self::$client_Secret,
"redirect_uri" => self::$redirect_uri,
"application_name" => "Test AdWords System"
];
if(self::checkForAccess() && self::isLoggedIn()){
$param["access_token"] = self::$access['access_token'];
}
//Create and Request to access Google API
$client = new Google_Client($params);
}
return $client;
}
public static function doLogin(){
$scopes = [ 'https://www.googleapis.com/auth/adwords', 'https://www.googleapis.com/auth/dfp', "https://www.googleapis.com/auth/userinfo.email"];
return self::GetClient()->createAuthUrl($scopes);
}
public static function doLoginFinal(){
if (!$code = $_GET['code']) {
throw new Exception("Auth Code is missing.");
}
$authResponse = self::GetClient()->authenticate($code);
if (isset($authResponse['error'])) {
throw new Exception(
"Unable to get access token.",
null,
new Exception(
"{$authResponse['error']} {$authResponse['error_description']}"
)
);
}
$_SESSION['GoogleAuth'] = $authResponse;
self::$access = $authResponse;
}
public static function isLoggedIn(){
if(self::checkForAccess()){
if(isset(self::$access)){
$expiresAt = @self::$access['created']+@self::$access['expires_in'];
return (time() < $expiresAt);
}
}
return false;
}
public static function GetExpiry(){
if(self::checkForAccess()){
return self::$access['created']+self::$access['expires_in'];
}
throw new Exception("The User is not logged into a google account.");
}
}
现在这个 class 正在工作我可以登录并且我有 google-adwords 的范围问题是由于 googleads-php-lib[ 的文档不完善造成的=24=]
所以从示例到 getCampaigns
it uses $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
but i don't have a file so i went into the OAuth2TokenBuilder
文件,我无法弄清楚如何将已经生成的访问令牌提供给 googleads
对象。
我仔细检查了 google-php-api-client
services repo,没有我可以使用的 adwords 服务。
我一直在研究 googleads-php-lib
的源文件,看看我是否能找到实现它的方法,但到目前为止我只是被卡住了,因为一切似乎都需要特定的参数类型,所以我可以装配一些东西来提供细节,但代码似乎总是依赖于多个 class,所以我不能只构建一个扩展 class 的代码。我通过了。
测试成功后密钥将被销毁!
经过数天对源文件的挖掘和修改,我终于找到了一个可行的实现。
创建我的经理帐户后:
https://developers.google.com/adwords/api/docs/guides/signup
这是添加到我的 GoogleClient Static 的两个新方法 Class
private static $developerToken = "";
private static function GetUserRefreshCredentials(){
return new UserRefreshCredentials(
null,
[
'client_id' => self::$client_id,
'client_secret' => self::$client_secret,
'refresh_token' => self::$access['refresh_token']
]
);
}
public function GetAdwordsSession(){
$builder = new AdWordsSessionBuilder();
$builder->defaultOptionals();
$builder->withDeveloperToken(slef::$developerToken);
return $builder->withOAuth2Credential(self::GetUserRefreshCredentials())->build();
}
所以我正在为 PHP 使用 Google API 客户端并且我有一个有效的 OAuth 流程,
class GoogleClient {
private static $client_id = "1050479587066-f64vq210hc2m15fdj4r77g8ml7jin30d.apps.googleusercontent.com";
private static $client_Secret = "CK8orQfPNpD9UgF0bqNJinVI";
private static $redirect_uri = '/return.php';
private static $access;
private static $client = null;
private static function checkForAccess(){
if(isset(self::$access)){
return true;
}
if(isset($_SESSION['GoogleAuth'])){
self::$access = $_SESSION['GoogleAuth'];
return true;
}
return false;
}
public static function GetClient(){
if(is_null(self::$client)){
$params = [
"client_id" => self::$client_id,
"client_secret" => self::$client_Secret,
"redirect_uri" => self::$redirect_uri,
"application_name" => "Test AdWords System"
];
if(self::checkForAccess() && self::isLoggedIn()){
$param["access_token"] = self::$access['access_token'];
}
//Create and Request to access Google API
$client = new Google_Client($params);
}
return $client;
}
public static function doLogin(){
$scopes = [ 'https://www.googleapis.com/auth/adwords', 'https://www.googleapis.com/auth/dfp', "https://www.googleapis.com/auth/userinfo.email"];
return self::GetClient()->createAuthUrl($scopes);
}
public static function doLoginFinal(){
if (!$code = $_GET['code']) {
throw new Exception("Auth Code is missing.");
}
$authResponse = self::GetClient()->authenticate($code);
if (isset($authResponse['error'])) {
throw new Exception(
"Unable to get access token.",
null,
new Exception(
"{$authResponse['error']} {$authResponse['error_description']}"
)
);
}
$_SESSION['GoogleAuth'] = $authResponse;
self::$access = $authResponse;
}
public static function isLoggedIn(){
if(self::checkForAccess()){
if(isset(self::$access)){
$expiresAt = @self::$access['created']+@self::$access['expires_in'];
return (time() < $expiresAt);
}
}
return false;
}
public static function GetExpiry(){
if(self::checkForAccess()){
return self::$access['created']+self::$access['expires_in'];
}
throw new Exception("The User is not logged into a google account.");
}
}
现在这个 class 正在工作我可以登录并且我有 google-adwords 的范围问题是由于 googleads-php-lib[ 的文档不完善造成的=24=]
所以从示例到 getCampaigns
it uses $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
but i don't have a file so i went into the OAuth2TokenBuilder
文件,我无法弄清楚如何将已经生成的访问令牌提供给 googleads
对象。
我仔细检查了 google-php-api-client
services repo,没有我可以使用的 adwords 服务。
我一直在研究 googleads-php-lib
的源文件,看看我是否能找到实现它的方法,但到目前为止我只是被卡住了,因为一切似乎都需要特定的参数类型,所以我可以装配一些东西来提供细节,但代码似乎总是依赖于多个 class,所以我不能只构建一个扩展 class 的代码。我通过了。
测试成功后密钥将被销毁!
经过数天对源文件的挖掘和修改,我终于找到了一个可行的实现。
创建我的经理帐户后: https://developers.google.com/adwords/api/docs/guides/signup
这是添加到我的 GoogleClient Static 的两个新方法 Class
private static $developerToken = "";
private static function GetUserRefreshCredentials(){
return new UserRefreshCredentials(
null,
[
'client_id' => self::$client_id,
'client_secret' => self::$client_secret,
'refresh_token' => self::$access['refresh_token']
]
);
}
public function GetAdwordsSession(){
$builder = new AdWordsSessionBuilder();
$builder->defaultOptionals();
$builder->withDeveloperToken(slef::$developerToken);
return $builder->withOAuth2Credential(self::GetUserRefreshCredentials())->build();
}