如何访问Google Calendar Api返回的JSON对象

时间:2016-02-22 02:02:16

标签: php error-handling exception-handling google-api google-api-php-client

我正在使用这段代码来捕获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);
        }

$ex->getMessage()正常工作并以字符串形式返回异常消息,但格式如下。我从来没有遇到像这样的数组。它看起来像JSON但似乎格格不入。如何在没有剩下的情况下访问/打印“无效的电子邮件或用户ID”。

    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] =>

1 个答案:

答案 0 :(得分:0)

这是我在朋友的帮助下现在的工作方式。希望它可以帮助别人。您可以将其直接与您的代码内联,如下所示,或将其放入函数中以重复使用它。

@for $i from 0 through 153 {
 $base: 865px;
 $inc: 150px;
.enrollments__inner > div[data-reactid$='#{$i}'] {
    position: absolute;
    @if $i >= 0 and $i <= 20 {
        left: (80 * $i) + px;
        top: $base;
    }
    @if $i >= 21 and $i <= 40 {
        left:  (80 * ($i - 21)) + px;
        top: $base + $inc;
    }
    @if $i >= 41 and $i <= 60 {
        left: (80 * ($i - 41)) + px;
        top: $base + ($inc * 2);
    }
    @if $i >= 61 and $i <= 80 {
        left: (80 * ($i - 61)) + px;
        top: $base + ($inc * 3);
    }
    @if $i >= 81 and $i <= 100 {
        left: (80 * ($i - 81)) + px;
        top: $base + ($inc * 4);
    }
    @if $i >= 101 and $i <= 121 {
        left: (80 * ($i - 101)) + px;
        top: $base + ($inc * 5);
    }
    @if $i >= 122 and $i <= 142 {
        left: (80 * ($i - 122)) + px;
        top: $base + ($inc * 6);
    }
    @if $i >= 143 and $i <= 153 {
        left: (80 * ($i - 143)) + px;
        top: $base + ($inc * 7);
    }
}

然后,只需使用

即可访问JSON中的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)); }
error_description