【问题标题】:Unable to decode credential from JWT无法从 JWT 解码凭证
【发布时间】:2021-12-03 14:23:05
【问题描述】:

我正在尝试实现新的“使用 Google 登录”按钮,如 https://developers.google.com/identity/gsi/web/guides/display-button 中所述。

一切都很好,我可以从带有“credential”和“g_csrf_token”元素的按钮获得响应,我可以将其发送到我的服务器。但是,使用 Google 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。

我假设我在某个地方错过了一步 - 有人可以帮忙吗?

【问题讨论】:

    标签: php jwt google-signin google-identity


    【解决方案1】:

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

    如果您在 Google 工作并了解此内容,如果您更新了文档,那就太好了。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2017-05-27
      • 2021-06-30
      • 2020-09-18
      • 2020-12-01
      • 2021-01-14
      • 2020-04-10
      • 2018-07-26
      • 2022-07-05
      相关资源
      最近更新 更多