无法从JWT解码凭证



我正在尝试实现新的"Sign in with Google"按钮,如https://developers.google.com/identity/gsi/web/guides/display-button.

一切都很好,我能够从带有"凭据"的按钮获得响应。和";g_csrf_token"元素,我可以将其发送到服务器。但是,使用Google API Client对凭证进行解码是行不通的。我正试着按照说明去做呢。

下面是我的代码:

$id_token = filter_input(INPUT_POST, 'credential');
$csrfToken = filter_input(INPUT_POST, 'g_csrf_token'); //??? Do we need this?

$client = new Google_Client(['client_id' => $clientid]);
$client->addScope("email"); // Recommended in another StackOverflow answer but makes no difference
try {
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload

结果是错误信息id_token must be passed in or set as part of setAccessToken

我已经更新了我的Google API Client到v2.11.

我想我错过了一个步骤-有人可以帮助吗?

已经找到了解决方案,通过试验和错误!事实证明,$id_token需要传递给客户端两次,一次在setAccessToken(),然后再次在verifyIdToken()。省略setAccessToken失败(如错误消息所说),但如果您在setAccessToken中传递它,但在verifyIdToken中不传递它,那也不起作用。

$id_token = filter_input(INPUT_POST, 'credential');
$client = new Google_Client(['client_id' => $clientid]);
try {
$client->setAccessToken($id_token);
$payload = $client->verifyIdToken($id_token);
} catch(Exception $ex) {
$errorMessage = "Error in verifyIdToken():" . $ex->getMessage();
// ...do stuff with the error message
}
// ...do stuff with the returned payload

如果你在谷歌,如果你更新了文档,那就太好了。

$client = new Google_Client([**'client_id'** => $clientid]);

"client_id">必须是您的谷歌客户端id从https://console.cloud.google.com/apis/credentials/oauthclient/

你需要注册你的web应用程序在谷歌云

最新更新