【发布时间】: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 的源文件,看看是否可以找到实现此功能的方法,但到目前为止我只是卡住了,因为一切似乎都需要特定的参数类型,所以我可以安装一些东西来提供详细信息,但代码似乎总是依赖于多个类,所以我不能只构建一个扩展类的类。我通过了。
此测试运行后,密钥将被销毁!
【问题讨论】:
标签: php google-api google-ads-api google-oauth