dsxsou8465 2018-11-02 14:11
浏览 188

如何将Google服务oAuth2转换为Google Ads API oAuth2访问权限

So I'm working with Google API Client for PHP and I have an OAuth flow that works,

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(){
            return true;

            self::$access = $_SESSION['GoogleAuth'];
            return true;
        return false;

    public static function GetClient(){
            $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.", 
                new Exception(
                    "{$authResponse['error']} {$authResponse['error_description']}"

        $_SESSION['GoogleAuth'] = $authResponse;
        self::$access = $authResponse;

    public static function isLoggedIn(){
                $expiresAt = @self::$access['created']+@self::$access['expires_in'];
                return (time() < $expiresAt);
        return false;

    public static function GetExpiry(){
            return self::$access['created']+self::$access['expires_in'];
        throw new Exception("The User is not logged into a google account.");

now this class is working I'm able to log in and I have the scope for google-adwords the problem comes about due to poor documentation for the googleads-php-lib

So from the example to getCampaigns it uses $oAuth2Credential = (new OAuth2TokenBuilder())->fromFile()->build(); but i don't have a file so i went into the OAuth2TokenBuilder file I'm unable to work out how i could give the already generated access tokens to the googleads objects.

I have double checked the google-php-api-client services repo and there is no adwords Service I can use.

I have been digging through the source files of the googleads-php-lib to see if I can find a method to implement this but so far I'm just getting stuck as everything seems to require specific parameter types so I can rig something to provide the details, but the code always seems to rely on multiple classes so I can't just build one that extends a class. and i pass that through.

Keys will be destoried after this test is working!

  • 写回答

1条回答 默认 最新

  • douxin9135 2018-11-06 14:03

    Well after days of digging around source files and hacking this and that I finally found an implementation that works.

    After creating my manager account: https://developers.google.com/adwords/api/docs/guides/signup

    So this is the two new methods added to my GoogleClient Static Class

    private static $developerToken = "";
    private static function GetUserRefreshCredentials(){
        return new UserRefreshCredentials(
                    'client_id' => self::$client_id,
                    'client_secret' => self::$client_secret,
                    'refresh_token' => self::$access['refresh_token']
    public function GetAdwordsSession(){
        $builder = new AdWordsSessionBuilder();
        return $builder->withOAuth2Credential(self::GetUserRefreshCredentials())->build();
    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 w10部分软件不能联网
  • ¥15 关于安装hbase的问题(操作系统-windows)
  • ¥15 cadence617版本,如何做一个参数可调的反相器
  • ¥15 novnc连接pve虚拟机报错安全协议不支持262
  • ¥15 设备精度0.03给多少公差能达到CPK1.33
  • ¥15 qt+ffmpeg报错non-existing PPS 0 referenced
  • ¥15 FOC simulink
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配