PHP Google API 客户端不一致未加载正确的凭据
PHP Google API Client Inconsistency not loading proper credentials
基本上,我一直在使用 PHP Google API 客户端库来访问多个用户(允许通过 oAuth2 工作流访问)的 gmail 收件箱并提取一些数据...
观察:
客户端仅在 10 到 15 分钟后才能正确验证...
一旦令牌用于验证用户的 gmail 收件箱,该用户就可以访问数据。但是当下一个令牌用于验证另一个用户的 gmail 收件箱时,PHP google api 客户端不会重新加载但使用以前的用户(我怀疑它已被缓存/存储在会话中).
代码流程:
<?php
$tokenArray = array();
foreach($tokenArray as $token) {
$googleclient = getGoogleClient($token);
//$googleclient is stale even after the second iteration of the foreach loop
$service = new Google_Service_Gmail($googleclient);
$googleuser = "me";
//if we change the variable $googleuser to some email address "xyz@gmail.com", we get a "Delegation denied for xyz@gmail.com"
$userprofile = $service->users->getProfile($googleuser);
var_dump($userprofile);//same data
}
?>
如果我们将其从循环中取出并在 25 - 30 分钟后进行单独调用(或重新启动 apache 网络服务器),则第二个令牌将在客户端库中正确使用。
是因为 Google API PHP 客户端库正在缓存对象并重复使用它 30 分钟,还是我遗漏了什么?
我的问题是在处理多个令牌时我应该如何重新加载客户端?
是否有合适的方法/任何其他方法来初始化客户端?
你们有没有 PHP Google API 客户端库的任何代码片段...我已经在官方文档中检查过但无济于事...
感谢您的帮助
这个问题在我删除了 "php client library" 并从 git 下载了新的集合后得到了修复 "git clone -b v1-master https://github.com/google/google-api-php-client.git
“
并从下载的集合中链接 autoload.php 文件...("google-api-php-client/src/Google/autoload.php")
以前我使用 Composer 来构建它...但现在我根本没有使用 Composer 并且 Google API "PHP Client Library" 工作得很好...
我也遇到了新问题...
问题 1) 致命错误:未捕获的异常 'Google_Auth_Exception' 在 google-api-php-client/src/Google/Auth/OAuth2 中带有消息 'Could not json decode the token'。php'
解决方案:不传递令牌字符串的关联数组,而是按原样传递令牌字符串... $client->setAccessToken($tokenstring);然后就解决了。还要确保 json_encode() 字符串 return 从 $client->getAccessToken();在将其存储到数据库或文件之前
问题 2) 未捕获的异常 'Google_Auth_Exception' 消息为“获取 OAuth2 访问令牌时出错,消息:'invalid_grant: Code was already redeemed.'
解决方法: 之前,google api 调用 $client-> 时似乎 return 错误信息作为关联数组验证($代码);但现在它抛出一个错误...所以使用 try{}catch() 的错误处理解决了这个问题
问题 2) 的来源是 firefox 的预取功能...
firefox 尝试两次命中 url 进行预取,但它引发了异常...仍在努力解决问题 (2)
基本上,我一直在使用 PHP Google API 客户端库来访问多个用户(允许通过 oAuth2 工作流访问)的 gmail 收件箱并提取一些数据...
观察: 客户端仅在 10 到 15 分钟后才能正确验证... 一旦令牌用于验证用户的 gmail 收件箱,该用户就可以访问数据。但是当下一个令牌用于验证另一个用户的 gmail 收件箱时,PHP google api 客户端不会重新加载但使用以前的用户(我怀疑它已被缓存/存储在会话中).
代码流程:
<?php
$tokenArray = array();
foreach($tokenArray as $token) {
$googleclient = getGoogleClient($token);
//$googleclient is stale even after the second iteration of the foreach loop
$service = new Google_Service_Gmail($googleclient);
$googleuser = "me";
//if we change the variable $googleuser to some email address "xyz@gmail.com", we get a "Delegation denied for xyz@gmail.com"
$userprofile = $service->users->getProfile($googleuser);
var_dump($userprofile);//same data
}
?>
如果我们将其从循环中取出并在 25 - 30 分钟后进行单独调用(或重新启动 apache 网络服务器),则第二个令牌将在客户端库中正确使用。
是因为 Google API PHP 客户端库正在缓存对象并重复使用它 30 分钟,还是我遗漏了什么?
我的问题是在处理多个令牌时我应该如何重新加载客户端?
是否有合适的方法/任何其他方法来初始化客户端?
你们有没有 PHP Google API 客户端库的任何代码片段...我已经在官方文档中检查过但无济于事...
感谢您的帮助
这个问题在我删除了 "php client library" 并从 git 下载了新的集合后得到了修复 "git clone -b v1-master https://github.com/google/google-api-php-client.git “
并从下载的集合中链接 autoload.php 文件...("google-api-php-client/src/Google/autoload.php")
以前我使用 Composer 来构建它...但现在我根本没有使用 Composer 并且 Google API "PHP Client Library" 工作得很好...
我也遇到了新问题...
问题 1) 致命错误:未捕获的异常 'Google_Auth_Exception' 在 google-api-php-client/src/Google/Auth/OAuth2 中带有消息 'Could not json decode the token'。php'
解决方案:不传递令牌字符串的关联数组,而是按原样传递令牌字符串... $client->setAccessToken($tokenstring);然后就解决了。还要确保 json_encode() 字符串 return 从 $client->getAccessToken();在将其存储到数据库或文件之前
问题 2) 未捕获的异常 'Google_Auth_Exception' 消息为“获取 OAuth2 访问令牌时出错,消息:'invalid_grant: Code was already redeemed.'
解决方法: 之前,google api 调用 $client-> 时似乎 return 错误信息作为关联数组验证($代码);但现在它抛出一个错误...所以使用 try{}catch() 的错误处理解决了这个问题
问题 2) 的来源是 firefox 的预取功能...
firefox 尝试两次命中 url 进行预取,但它引发了异常...仍在努力解决问题 (2)