php 无法从JWT解码凭据

fjaof16o  于 2023-06-04  发布在  PHP
关注(0)|答案(2)|浏览(310)

我正在尝试实现新的“登录与谷歌”按钮中所描述的https://developers.google.com/identity/gsi/web/guides/display-button
一切都很好,我能够从按钮中获得包含“credential”和“g_csrf_token”元素的响应,我可以将其发送到我的服务器。但是,使用API客户端来解码凭据不起作用。我在试着追踪instructions
下面是我的代码:

$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客户端到v2.11。
我想我在某个地方漏掉了一个步骤--有人能帮忙吗?

nbysray5

nbysray51#

通过反复试验找到了解决办法!原来$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

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

cunj1qz1

cunj1qz12#

排队

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

您需要在Google Cloud中注册您的Web应用程序

相关问题