google-ads-php icon indicating copy to clipboard operation
google-ads-php copied to clipboard

PHP Client: Persistent `Call to undefined method OAuth2TokenBuilder::withJsonKey()` despite v26 & PHP 8.2

Open DesignMike opened this issue 9 months ago • 1 comments

Hi Team,

I'm encountering a persistent issue using the googleads/google-ads-php client library (v26.0.0) with PHP 8.2.x for Service Account authentication within a WordPress plugin.

Despite upgrading PHP and ensuring the latest library version is installed, I consistently receive the following fatal error:

Fatal error: Uncaught Error: Call to undefined method Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder::withJsonKey() in /path/to/my/plugin/includes/class-google-ads-api-handler.php on line 58

Relevant Code:

I'm retrieving the service account JSON content (which was originally uploaded by the user and stored as a string in the WordPress options table) and decoding it into an array ($jsonData). Then, I attempt to build the credentials like this:

// includes/class-google-ads-api-handler.php (around lines 37-59)

// Ensure service account JSON is properly decoded
$jsonData = get_option('gad_service_account_credentials'); // Get JSON string from WP DB
$decodedJson = json_decode($jsonData, true);

if (json_last_error() !== JSON_ERROR_NONE || !is_array($decodedJson)) {
    error_log('Error decoding service account JSON: ' . json_last_error_msg());
    // Handle error...
    return;
}
$jsonData = $decodedJson; // $jsonData is now a PHP array

// Configure the OAuth2 token builder with service account credentials (using FQN)
// LINE 58 BELOW IS WHERE THE ERROR OCCURS
$oAuth2Credential = (new \Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder())
    ->withJsonKey($jsonData)
    ->build();

// ... rest of client building using $oAuth2Credential

Troubleshooting Steps Taken:

  1. PHP Version Confirmed: Server is running PHP 8.2.x.
  2. Library Version Confirmed: composer show googleads/google-ads-php confirms v26.0.0 is installed.
  3. Fresh Install: Deleted the entire vendor directory and composer.lock file, then ran a fresh composer install. Uploaded the complete new vendor directory.
  4. OpCache Cleared: Restarted PHP-FPM service to ensure OpCache was cleared. Also tried disabling OpCache temporarily.
  5. Code Verification: Confirmed $jsonData being passed to withJsonKey is a valid PHP array decoded from the service account JSON.
  6. Method Exists: Manually verified that the method withJsonKey(array $jsonKey) does exist in the file vendor/googleads/google-ads-php/src/Google/Ads/GoogleAds/Lib/OAuth2TokenBuilder.php in the installed library on the server.
  7. Fully Qualified Namespace: Tried using the fully qualified namespace \Google\Ads\GoogleAds\Lib\OAuth2TokenBuilder() as shown above.
  8. File Permissions: Checked that web server has read access to the vendor directory files.
  9. No Conflicts Found: Ensured no other versions of the plugin or conflicting libraries seem to be loaded. The error trace points directly to the expected file.

Despite all this, PHP acts as if the withJsonKey method doesn't exist on the OAuth2TokenBuilder class. I'm running out of ideas for what could be causing PHP to fail to see this method.

Could there be any other environmental factor, PHP extension issue, or subtle autoloader conflict I might be missing? Any suggestions on further debugging steps would be greatly appreciated.

Thanks!

DesignMike avatar Apr 14 '25 16:04 DesignMike

Hi,

Thank you for this detailed issue! Could you try updating the google_ads_php.ini file (https://github.com/googleads/google-ads-php/blob/main/examples/Authentication/google_ads_php.ini) on your local machine, commenting out the installed application flow, and filling in your information the service account flow instead?

Then can you try running a sample from our library like GetCampaigns (https://github.com/googleads/google-ads-php/blob/main/examples/BasicOperations/GetCampaigns.php), and let me know if it works/what errors you receive? Thank you!

Brothman avatar Apr 15 '25 14:04 Brothman