如何将 Google Services oAuth2 转换为 Google Ads API oAuth2 访问权限
Posted
技术标签:
【中文标题】如何将 Google Services oAuth2 转换为 Google Ads API oAuth2 访问权限【英文标题】:How do you turn a Google Services oAuth2 into a Google Ads API oAuth2 access 【发布时间】:2018-11-02 14:11:37 【问题描述】:所以我正在使用适用于 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.");
现在这个课程正在运行,我可以登录,并且我有 google-adwords 的范围,由于googleads-php-lib 的文档不佳,问题出现了
所以从示例到getCampaigns
它使用$oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build();
但我没有文件所以我进入OAuth2TokenBuilder
文件我无法弄清楚如何将已经生成的访问令牌提供给googleads
对象。
我已经仔细检查了google-php-api-client
services repo,没有可以使用的 adwords 服务。
我一直在挖掘googleads-php-lib
的源文件,看看是否可以找到实现此功能的方法,但到目前为止我只是卡住了,因为一切似乎都需要特定的参数类型,所以我可以安装一些东西来提供详细信息,但代码似乎总是依赖于多个类,所以我不能只构建一个扩展类的类。我通过了。
此测试运行后,密钥将被销毁!
【问题讨论】:
【参考方案1】:经过几天对源文件的挖掘和破解,我终于找到了一个可行的实现。
创建经理帐号后: https://developers.google.com/adwords/api/docs/guides/signup
这是添加到我的 GoogleClient 静态类中的两个新方法
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();
【讨论】:
以上是关于如何将 Google Services oAuth2 转换为 Google Ads API oAuth2 访问权限的主要内容,如果未能解决你的问题,请参考以下文章
生成 google-services.json 时 SHA 密钥冲突的问题
如何在 GitHub Actions 的自托管运行器上修复“GitHub.Services.OAuth.VssOAuthTokenRequestException”
对 ASP.NET Core 中的 Google OAuth 包感到困惑