PHP Google_Auth_Exception: "Wrong recipient" 当使用 Google API PHP 客户端时

PHP Google_Auth_Exception: "Wrong recipient" when using Google API PHP client

尝试在 Google API PHP 客户端中使用 verifyIdToken() 时,出现以下错误。

PHP Fatal error:  Uncaught exception 'Google_Auth_Exception' with message 'Wrong recipient, **API KEY OMMITED FOR LENGTH**.apps.googleusercontent.com != :' in C:\includes\gplus\src\Google\Auth\OAuth2.php:621
Stack trace:
#0 C:\includes\gplus\src\Google\Auth\OAuth2.php(497): Google_Auth_OAuth2->verifySignedJwtWithCerts('**TOKEN OMMITED**', Array, '', Array)
#1 C:\includes\gplus\src\Google\Client.php(482): Google_Auth_OAuth2->verifyIdToken('**TOKEN OMMITED**')
#2 C:\tjWEB\googlesignin\token.php(8): Google_Client->verifyIdToken('**TOKEN OMMITED**')
#3 {main}
   thrown in C:\includes\gplus\src\Google\Auth\OAuth2.php on line 621

我的PHP代码如下:

<?php
require_once 'C:/includes/gplus/src/Google/autoload.php';

$client = new Google_Client();
$client->setApplicationName("My Application");
$client->setDeveloperKey("**TOKEN OMMITED**");

var_dump($client->verifyIdToken($_POST["idtoken"]));
?>

调用它的页面可用here。我假设可能正在阅读本文的每个人都知道如何在他们选择的浏览器中查看源代码。

我在 Google 上找不到任何解决方案,因此我们将不胜感激。 谢谢!

好的,我找到了导致这个问题的原因。

我需要添加

$client->setAuthConfigFile('C:/includes/client_secret.JSON');

到 PHP 代码,它给了它我的 API 密钥。如果您还没有密钥,可以下载密钥 here

TL;DR 我没看文档搞砸了