您如何将Google Services oAuth2转换为Google Ads API oAuth2访问权限

时间:2018-11-02 14:11:38

标签: php google-api google-adwords google-oauth2

所以我正在使用适用于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 个答案:

答案 0 :(得分:0)

经过数天的源文件挖掘和破解后,我终于找到了可行的实现。

创建我的经理帐户后:  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();
}