WordPress.org

Ready to get started?Download WordPress

Forums

Google Analyticator
Solution for "Fatal error: Uncaught exception 'Google_AuthException'" (4 posts)

  1. Jeremy Clarke
    Member
    Posted 1 year ago #

    This error was mentioned in other threads but I'm starting a new one because I have tested this and it still affects the latest version (6.4.3). I also have a code solution below.

    I first noticed the error on sites where I had Google Analyticator installed previously, updated the plugin (and WP to 3.5 simultaneously) and everything seemed to work. At some point the authorization broke and the Fatal Error started happening. My site managers noticed because they could not edit their widgets.

    This particular message affects both the GA settings screen and the widgets screen. In both cases the page only renders until the time the exception is "uncaught", resulting is completly broken screens. This means that when the bug happens in the GA settings users are unable to do ANYTHING. All you can see is the "enabled/disabled" select menu and the error. There's no way to even act on the select menu because the form is not complete and cannot be submitted. Same goes for the widgets, which cannot be looked at or modified at all.

    Here is an example of the error:

    Fatal error: Uncaught exception 'Google_AuthException' with message 'Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'' in /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php:279 Stack trace: #0 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php(239): Google_OAuth2->refreshTokenRequest(Array) #1 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php(216): Google_OAuth2->refreshToken('1/8JhPDkzVXTxkR...') #2 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/service/Google_ServiceResource.php(166): Google_OAuth2->sign(Object(Google_HttpRequest)) #3 /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/contrib/Google_AnalyticsService.php(54): Google_ServiceResource->__call('list', Array) #4 in /path/to/site/wp-content/plugins/google-analyticator/google-api-php-client/src/auth/Google_OAuth2.php on line 279

    As far as I can tell the Google_Oauth2.php file is not one you wrote, but rather an API file from Google. Either way the bug is in that file. If you got it from somewhere else please repost a version of this report for them to read and hopefully fix the problem upstream, but for now you should probably fix it in the plugin's copy.

    I am not experienced at all in the try/catch syntax of PHP (it is very modern compared to most PHP in WP-land) but I read the PHP documentation about exceptions and based on that it seems like the Google_OAuth2.php file is simply in violation of the fundamental precepts of a try/catch loop.

    "If an exception is not caught, a PHP Fatal Error will be issued with an "Uncaught Exception ..." error"

    In Google_OAuth2::refreshToken() the method Google_OAuth2::refreshTokenRequest() is called, and refreshTokenRequest() "throws" exceptions in various scenarios where something is wrong. The problem is that the calling function (Google_OAuth2::refreshToken()) makes no attempt to "catch" these exceptions. It just stupidly fails with a fatal error and ruins the ability of users to interact with Google Analytics settings form.

    Now because I'm not familiar with Exceptions in general and the convoluted setup in Google_Oauth2.php in particular I don't know the best way to solve this. The PHP docs mention set_exception_handler() as a potential avenue, but it seems like the real way to fix this is to have proper try/catch structures whenever a function is called that might throw an exception.

    I fixed my copy of Google_OAuth2.php so that the particular uncaught exception that was giving me problems would have a try/catch in it. This solved my problem and even though the form is still confusing, there is no longer a fatal error, so I can click the "reset+dauthorize" button at the bottom of the Analyticator settings and get things workign again.

    Here are my changes as a gist:
    https://gist.github.com/jeremyclarke/4724011

    FWIW if you can fix these try/catch errors you will be able to at least tell people to just use the reset link. It's these fatal errors mid-pageview that are forcing you to tell people to mess around in the database where they dont' belong.

    Overall you should also attempt to fix the issues themselves in the OAuth system that cause the exceptions, and handle those outcomes better (e.g. by having an explicit link saying "you should reset your authorization"), but the priority IMHO is catching these exceptions.

    http://wordpress.org/extend/plugins/google-analyticator/

  2. Video User Manuals
    Member
    Plugin Author

    Posted 1 year ago #

    Hey Jeremy,

    Thanks so much for your time on this, it's much appreciated.

    You're right, it's the google classes that are causing this error, and as far as I know, all the calls we make,I try catch any errors.

    The only problem here is that if and when Google update their docs, we're going to be constantly chasing our tail trying to keep merging custom fixes in.

    I wonder if it'd be worth submitting to the Google Repo?

    http://code.google.com/p/google-api-php-client/source/browse/trunk/src/auth/Google_OAuth2.php

    cheers,
    Ben

  3. Video User Manuals
    Member
    Plugin Author

    Posted 1 year ago #

    Thanks Jeremy. A little further in our roadmap we are looking at re-writing the codebase, as it's fully of anomalies, we'd also address the above issues too.

  4. Jeremy Clarke
    Member
    Posted 1 year ago #

    Hey guys.

    I checked the google code site for the API and it seems like they've had some small incremental updates and are now at v0.6.2:
    https://code.google.com/p/google-api-php-client/downloads/list

    Your changelog doesn't mention you guys updating the API to the new version, though when I check out the offending file in their source it seems the try/catch failure is still there:

    https://code.google.com/p/google-api-php-client/source/browse/trunk/src/auth/Google_OAuth2.php#237

    Too bad about that. I wish they used GitHub instead of Google Code!

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.