PHP Google API 客户端不一致未加载正确的凭据


PHP Google API Client Inconsistency not loading proper credentials

基本上,我一直在使用PHP Google API 客户端库来访问多个用户的 gmail 收件箱(他们允许通过 oAuth2 工作流程访问)并提取一些数据......

观察:客户端仅在 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 Web 服务器),则第二个令牌在客户端库中正确使用。

是因为 Google API PHP 客户端库正在缓存对象并将其重用 30 分钟还是我错过了什么?

我的问题是,在处理多个令牌时,我应该怎么做才能重新加载客户端?

是否有正确的方法/任何其他方法来初始化客户端?

你们中是否有任何PHP谷歌API客户端库的代码片段...我已经在官方文档中检查过它,但无济于事......

感谢您的任何帮助

在我删除"php 客户端库"并从 git 下载新集"git clone -b v1-master https://github.com/google/google-api-php-client.git'"

并从下载的集合中链接了自动加载.php文件...("google-api-php-client/src/Google/autoload.php")

以前我使用作曲家来构建它...但现在我根本没有使用作曲家,谷歌 API"PHP 客户端库"工作得很好......


我也遇到了新的问题...

问题 1)致命错误:未捕获的异常"Google_Auth_Exception",在google-api-php-client/src/Google/Auth/OAuth2中显示消息"无法json解码令牌.php"

解决方案:不要传递令牌字符串的关联数组,而是按原样传递令牌字符串... $client->setAccessToken($tokenstring); 然后解决。 还要确保json_encode() 从 $client->getAccessToken() 返回的字符串; 在将其存储在数据库或文件中之前

问题 2)未捕获的异常"Google_Auth_Exception",消息"获取 OAuth2 访问令牌时出错,消息:"invalid_grant:代码已兑换"。

解决方案:以前,当我们调用 $client->authenticate($code) 时,Google API 似乎将错误消息作为关联数组返回;但现在它抛出了一个错误......所以使用 try{}catch() 的错误处理解决了这个问题

问题 2) 的来源是 Firefox 的预取功能...

Firefox 尝试两次点击 URL 进行预取,并引发了异常......仍在努力克服问题 (2)