1

I am using this bit of code to catch Exceptions from the Google Calendar API

//authorize the client
      try{
          $client = $this->gcal_sync_auth($crm_user_id);
        }
      catch (Exception $ex) 
        {
      $message = $ex->getMessage();
      die("could not connect ". $message);
        }

The $ex->getMessage() works fine and returns the exception message as a string, but it is formatted as below. I have never encountered an array like this. It looks like JSON but seems malformed. How can I just access/print the "Invalid email or User ID" without the rest.

    Google_Auth_Exception Object
(
    [message:protected] => Error refreshing the OAuth2 token, message: '{
  "error" : "invalid_grant",
  "error_description" : "Invalid email or User ID"
}'
    [string:Exception:private] => 
    [code:protected] => 400
    [file:protected] => xxx/application/third_party/Google_API/src/Google/Auth/OAuth2.php
    [line:protected] => 364
    [trace:Exception:private] => Array
        (
            [0] => Array
                (
                    [file] => xxx/application/third_party/Google_API/src/Google/Auth/OAuth2.php
                    [line] => 315
                    [function] => refreshTokenRequest
                    [class] => Google_Auth_OAuth2
                    [type] => ->
                    [args] => Array
                        (
                            [0] => Array
                                (
                                    [grant_type] => assertion
                                    [assertion_type] => http://oauth.net/grant_type/jwt/1.0/bearer
                                    [assertion] =>
                                    [serviceAccountName] =>              xxxxx@xxxxxx.iam.gserviceaccount.com
                                    [scopes] => https://www.googleapis.com/auth/calendar
                                    [privateKey] =>
Cœur
  • 37,241
  • 25
  • 195
  • 267
skribe
  • 3,595
  • 4
  • 25
  • 36
  • Read error message and follow what it tells. Reset your outh2 key, write valid email – Monty Feb 23 '16 at 09:10
  • Thanks. But did you even read my question? I am trying to parse the exception message so I can print out something more intelligible to the end user. – skribe Feb 23 '16 at 09:19
  • what you getting if you do like this : print_r($ex); – Monty Feb 23 '16 at 09:32
  • The same thing but more arrays below it. – skribe Feb 23 '16 at 09:35
  • pls show full error array. did you get something like this : https://developers.google.com/google-apps/calendar/v3/errors – Monty Feb 23 '16 at 09:39
  • I just wanted to see what error code you are getting > – Monty Feb 23 '16 at 09:51
  • Ok I update the code above with the contents of the $ex var to the first index. – skribe Feb 23 '16 at 09:51
  • I can get the whole message string with $ex->getMessage(); or I can get the error code with $ex->getCode(); But the Message string looks like it is in JSON. But I can't find a way to just parse out the "error_description" – skribe Feb 23 '16 at 09:54
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/104255/discussion-between-monty-and-skribe). – Monty Feb 23 '16 at 09:59
  • Hope following links will help : http://stackoverflow.com/questions/9930843/get-string-within-protected-object http://stackoverflow.com/questions/20334355/how-to-get-protected-property-of-object-in-php – Shivani P Feb 23 '16 at 10:34

1 Answers1

0

Here is the way I have this working now with the help of a friend. Hope it helps someone else. You can put it directly inline with your code as below or put it into a function to reuse it.

try{
    $client = $this->gcal_sync_auth($crm_user_id);
   }
 catch (Google_Auth_Exception $ex) {
    $parts = explode("'", $ex->getMessage());
     array_pop($parts);
     array_shift($parts);
    $error = json_decode(implode("'", $parts));
 }

Then you can access the error_description in the JSON with simply with

$error->error_description
skribe
  • 3,595
  • 4
  • 25
  • 36