googleads / google-ads-php

Google Ads API Client Library for PHP
https://developers.google.com/google-ads/api/docs/client-libs/php
Apache License 2.0
280 stars 260 forks source link

PHP API V15 - Call to undefined method Google\Ads\GoogleAds\Lib\V15\GoogleAdsClient::useGapicV2Source() #993

Closed bdfcommunication closed 6 months ago

bdfcommunication commented 6 months ago

In the V15 api version there is a bug. OS: Debian 11 64Bit. PHP 8.0. Every other packet installed and up to date!

While the class ServiceClientFactoryTrait makes massive use of the call $this->useGapicV2Source(), the method seems not to be implemented anywhere.

Once correctely created the client by: $googleAdsClient = (new GoogleAdsClientBuilder())->fromFile() ->withOAuth2Credential($oAuth2Credential) ->withLoginCustomerId($loginCustomerId ?? $rootCustomerId) ->build(); The call: $googleAdsServiceClient = $googleAdsClient->getGoogleAdsServiceClient();

will result in fatal php error

Fatal error: Uncaught Error: Call to undefined method Google\Ads\GoogleAds\Lib\V15\GoogleAdsClient::useGapicV2Source() in directory../google-ads-api/src/Google/Ads/GoogleAds/Lib/V15/ServiceClientFactoryTrait.php:1337

The problem is simply solved by adding the method to the class GoogleAdsClient returning its setted value from the constructor so that GoogleAdsClient should seems should be:

class GoogleAdsClient { use ServiceClientFactoryTrait;

/**
 * Creates a Google Ads API client from the specified builder.
 *
 * Do not use this constructor; Instances should be created by using the
 * `GoogleAdsClientBuilder` instead.
 *
 * @param GoogleAdsClientBuilder $builder the builder to create an instance
 *     of this client from
 */
public function __construct(GoogleAdsClientBuilder $builder)
{
    $this->developerToken = $builder->getDeveloperToken();
    $this->useCloudOrgForApiAccess = $builder->useCloudOrgForApiAccess();
    $this->loginCustomerId = $builder->getLoginCustomerId();
    $this->linkedCustomerId = $builder->getLinkedCustomerId();
    $this->endpoint = $builder->getEndpoint();
    $this->oAuth2Credential = $builder->getOAuth2Credential();
    $this->logger = $builder->getLogger();
    $this->logLevel = $builder->getLogLevel();
    $this->proxy = $builder->getProxy();
    $this->transport = $builder->getTransport();
    $this->grpcChannelIsSecure = $builder->getGrpcChannelIsSecure();
    $this->grpcChannelCredential = $builder->getGrpcChannelCredential();
    $this->useGapicV2Source = $builder->useGapicV2Source();
    $this->unaryMiddlewares = $builder->getUnaryMiddlewares();
    $this->streamingMiddlewares = $builder->getStreamingMiddlewares();
    $this->grpcInterceptors = $builder->getGrpcInterceptors();

    // Initializes preemptively the GoogleAdsFailures type when
    // gRPC is not available.
    if (!self::getGrpcDependencyStatus()) {
        GoogleAdsFailures::init();
    }
}

//missing method // public function useGapicV2Source() { return $this->useGapicV2Source; } }

I know it is not a great deal but it can make you loose a lot of time, especially if you are not an expert and you update using composer and expect everything that worked to work again.

Sincerely

fiboknacky commented 6 months ago

That method is in a trait that GoogleAdsClient imports, so it should be available. How did you update your code up to this point? If you run composer update, it should update everything at once, including GoogleAdsClient and ConfigurationTrait.

gabrielmosermick commented 6 months ago

Just having the same error here in V14 when using: $googleAdsServiceClient = $this->googleAdsClient->getGoogleAdsServiceClient();

Composer is fully updated. ConfigurationTrait is also in the folder.

Here's the error: Uncaught Error: Call to undefined method Google\Ads\GoogleAds\Lib\V14\GoogleAdsClient::useGapicV2Source() in /var/www/admanager.reweb.com.br/vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/Lib/V14/ServiceClientFactoryTrait.php:1294

bdfcommunication commented 6 months ago

I tried adding the function to the ConfigurationTrait.php on /main/src/Google/Ads/GoogleAds/Lib/ConfigurationTrait.php, that no, it didn't have that line, and everything worked like a charm. If you notice, the file ConfigurationTrait.php you have from V13 and the one on github Link are different exactely on that method. I don't understand why composer does not update that but adding the method by hand solved the thing for me.

gabrielmosermick commented 6 months ago

Thanks @bdfcommunication !

fiboknacky commented 6 months ago

I take it that you can resolve this problem, so I'm closing this issue now. Feel free to open it if you still encounter this issue.

gabrielmosermick commented 6 months ago

I was checking and the function useGapicV2Source is already in ConfigurationTrait.php, so I'm not sure about that move. Besides, I don't know if changing vendor files is a good definitive 'solution'. I see that from V13 from V14 a lot of calls to $this->useGapicV2Source() are included in ServiceClientFactoryTrait.php . Maybe there's a bug @fiboknacky ?

fiboknacky commented 6 months ago

Besides, I don't know if changing vendor files is a good definitive 'solution'.

I'm not sure what you meant, but to properly upgrade the dependencies, you should always run composer update at the root of your project. If you're unsure, you can just remove vendor and composer.lock and run composer update again. Could you please try doing so?