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

13 views
Skip to first unread message

Mike B

unread,
Apr 10, 2025, 3:57:08 PM4/10/25
to Google Ads API and AdWords API Forum
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:

```php
// 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!

Google Ads API Forum Advisor

unread,
Apr 10, 2025, 5:02:49 PM4/10/25
to djmikeb...@gmail.com, adwor...@googlegroups.com

Hi,

Thank you for contacting the Google Ads API support team.

Since you are having the issue with the ‘PHP client library’ and this falls under the scope of the client library support team. I would recommend reaching out to them for further assistance. You can file an issue and get support by visiting the following URL: Google Ads API Client Library Issues(PHP).

Kindly note that our team specializes in Google Ads API related concerns and technical implementations only.

Thanks,
 
Google Logo Google Ads API Team

Feedback
How was our support today?

rating1    rating2    rating3    rating4    rating5
[2025-04-10 21:02:06Z GMT] This message is in relation to case "ref:!00D1U01174p.!500Ht01pSY3Z:ref" (ADR-00299482)



Reply all
Reply to author
Forward
0 new messages