在 Web 应用上集成 Google 登录按钮在 verifyIdToken() 步骤失败。

huangapple go评论122阅读模式
英文:

Integrating Google Login Button on web app fails at verifyIdToken() step

问题

我正在尝试使用 PHP 构建的 Web 应用程序上的 Google 登录按钮集成 Google Identity Services。我能够显示按钮并成功接收到令牌的 POST 请求。但在尝试验证令牌时,似乎发生了一个调用以从 Google 检索证书,但这似乎失败了。请帮助。
英文:

I am trying to integrate with Google Identity Services using the Google login button on a web app built with PHP. I am able to get the button to show and even receive the post with the token. But when trying to validate the token a call seems to be made to retrieve certs from Google but this appears to fail. Please help

[Wed Mar 15 12:29:59.074744 2023] [php:error] [pid 71] [client 172.17.0.1:43470] PHP Fatal error:  Uncaught RuntimeException: Unable to read from stream in /var/www/html/vendor/guzzlehttp/psr7/src/Stream.php:237
Stack trace:
#0 /var/www/html/vendor/guzzlehttp/psr7/src/Utils.php(53): GuzzleHttp\\Psr7\\Stream->read()
#1 /var/www/html/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(210): GuzzleHttp\\Psr7\\Utils::copyToStream()

#13 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(173): GuzzleHttp\\Client->get()
#14 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(201): Google\\AccessToken\\Verify->retrieveCertsFromLocation()
#15 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(105): Google\\AccessToken\\Verify->getFederatedSignOnCerts()
#16 /var/www/html/vendor/google/apiclient/src/Client.php(815): Google\\AccessToken\\Verify->verifyIdToken()
#17 /var/www/html/siohub/public/web/login.php(62): Google\\Client->verifyIdToken()
#18 {main}

Next GuzzleHttp\\Exception\\RequestException: Unable to read from stream in /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:52
Stack trace:
#10 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(173): GuzzleHttp\\Client->get()
#11 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(201): Google\\AccessToken\\Verify->retrieveCertsFromLocation()
#12 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(105): Google\\AccessToken\\Verify->getFederatedSignOnCerts()
#13 /var/www/html/vendor/google/apiclient/src/Client.php(815): Google\\AccessToken\\Verify->verifyIdToken()
#14 /var/www/html/siohub/public/web/login.php(62): Google\\Client->verifyIdToken()
#15 {main}
  thrown in /var/www/html/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 52, referer: http://localhost:8081/siohub/public/web/login.html

答案1

得分: 1

找到了解决方案并在这里发布供参考。

Google-Client使用GuzzleHttp进行API调用。在某些情况下,GuzzleHttp会进行异步调用(请参见下面的跟踪片段)。为了处理这个问题,需要使用['stream' => true]配置Guzzle-Client,然后将Guzzle-Client分配给Google-Client,如下所示。

$google = new Google\Client();
$guzzle = new GuzzleHttp\Client(['stream' => true]);
$google->setHttpClient($guzzle);
#11 /var/www/html/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\Client->requestAsync()
#12 /var/www/html/vendor/guzzlehttp/guzzle/src/ClientTrait.php(44): GuzzleHttp\Client->request()
#13 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(173): GuzzleHttp\Client->get()
#14 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(201): Google\AccessToken\Verify->retrieveCertsFromLocation()

Guzzle文档中似乎有一个解释,说明为什么Guzzle会切换到异步模式。文档指出,如果未安装cURL,Guzzle将使用PHP流包装器发送HTTP请求。

英文:

I found a solution for this and posting here for reference.

Google-Client uses GuzzleHttp to make api calls. In certain circumstances GuzzleHttp makes asynch calls (see trace snippet below). To handle this the Guzzle-Client needs to be configured with ['stream' => true] and then assign the Guzzle-Client to the Google-Client as per the following.

$google = new Google\Client();
$guzzle = new GuzzleHttp\Client(['stream' => true]);
$google->setHttpClient($guzzle);
#11 /var/www/html/vendor/guzzlehttp/guzzle/src/Client.php(187): GuzzleHttp\\Client->requestAsync()
#12 /var/www/html/vendor/guzzlehttp/guzzle/src/ClientTrait.php(44): GuzzleHttp\\Client->request()
#13 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(173): GuzzleHttp\\Client->get()
#14 /var/www/html/vendor/google/apiclient/src/AccessToken/Verify.php(201): Google\\AccessToken\\Verify->retrieveCertsFromLocation()

In the Guzzle Documentation there seems to be an explanation why guzzle switches to async mode. It says Guzzle will use the PHP stream wrapper to send HTTP requests if cURL is not installed.

huangapple
  • 本文由 发表于 2023年3月15日 21:06:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75745142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定