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(){
        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.");
    }
}

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(
                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();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)