[英]How to access JSON object returned by Google Calendar Api
I am using this bit of code to catch Exceptions from the Google Calendar API 我正在使用这段代码从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. $ex->getMessage()
可以正常工作,并以字符串形式返回异常消息,但是其格式如下。 I have never encountered an array like this. 我从未遇到过像这样的数组。 It looks like JSON but seems malformed. 看起来像JSON,但格式不正确。 How can I just access/print the "Invalid email or User ID" without the rest. 我如何才能访问/打印“无效的电子邮件或用户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] =>
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 然后,您只需使用即可访问JSON中的error_description
$error->error_description
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.