如何访问由 Google 日历 Api 返回的 JSON 对象
How to access JSON object returned by Google Calendar Api
我正在使用这段代码来捕获来自 Google 日历 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()
工作正常,returns 异常消息作为字符串,但格式如下。我从来没有遇到过这样的数组。它看起来像 JSON 但似乎格式不正确。我怎么能只 access/print "Invalid email or User 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] =>
这是我现在在朋友的帮助下工作的方式。希望它能帮助别人。您可以将其直接内联到您的代码中,如下所示,或者将其放入函数中以重用它。
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));
}
然后您可以通过
访问 JSON 中的 error_description
$error->error_description
我正在使用这段代码来捕获来自 Google 日历 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()
工作正常,returns 异常消息作为字符串,但格式如下。我从来没有遇到过这样的数组。它看起来像 JSON 但似乎格式不正确。我怎么能只 access/print "Invalid email or User 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] =>
这是我现在在朋友的帮助下工作的方式。希望它能帮助别人。您可以将其直接内联到您的代码中,如下所示,或者将其放入函数中以重用它。
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));
}
然后您可以通过
访问 JSON 中的error_description
$error->error_description