简体   繁体   English

验证 PHP 中的 ID 令牌时出错 - Google API

[英]Error validate an ID token in PHP - Google API

I am using the following examplebackend-auth Google我正在使用以下示例backend-auth Google

<?php
    require_once 'Google/vendor/autoload.php';

    $CLIENT_ID = "xxxxxxxxxx";

    // Get $id_token via HTTPS POST.

    $client = new Google_Client(['client_id' => $CLIENT_ID]);  // Specify the CLIENT_ID of the app that accesses the backend
    $payload = $client->verifyIdToken($id_token);
    if ($payload) {
        $userid = $payload['sub'];
        // If request specified a G Suite domain:
        //$domain = $payload['hd'];
    } else {
    // Invalid ID token
    }
?>

I have already created the credentials, API , and OAUTH2我已经创建了凭据APIOAUTH2

API_KEY: AIzaAsDfGuYn6nk9761kvnwMxns-PPeO1Ka1YsA
CLIENT_ID: 15123456862-94jrd0d2lis29lbl6dekpk0fp4otgm8r.apps.googleusercontent.com
CLIENT_SECRET: qertf3l3UfgdhjiWEREZI8xN

But it generates the following error:但它会产生以下错误:

Notice: Undefined variable: id_token in C:\Adsense\index.php on line 10

Fatal error: Uncaught LogicException: id_token must be passed in or set as part of setAccessToken in C:\Adsense\Google\vendor\google\apiclient\src\Client.php:784 Stack trace: #0 C:\Adsense\index.php(10): Google\Client->verifyIdToken(NULL) #1 {main} thrown in C:\Adsense\Google\vendor\google\apiclient\src\Client.php on line 784

I have searched this forum in the google documentation and in the google console panel but I cannot find a fixed token or how to create it, I cannot find references我在谷歌文档和谷歌控制台面板中搜索了这个论坛,但我找不到固定令牌或如何创建它,我找不到参考

https://oauth2.googleapis.com/tokeninfo?id_token=XXXXX

It's a bit of an old post, but for anyone like me and the OP struggling with this, it's actually quite a simple solution.这是一个有点旧的帖子,但对于像我和 OP 这样苦苦挣扎的人来说,这实际上是一个非常简单的解决方案。 The example code has a commented out line that reads示例代码有一个注释掉的行,内容为

    // Get $id_token via HTTPS POST.

You actually have to write this code.您实际上必须编写此代码。 Using the worked examples from Google, the JavaScript function passes the authentication token as idtoken.使用来自 Google 的工作示例,JavaScript function 将身份验证令牌作为 idtoken 传递。

    $id_token = $_POST['idtoken'];

Will do the trick.会成功的。 You'll probably want to perform some basic error checking, for example,您可能需要执行一些基本的错误检查,例如,

    if (isset($_POST['idtoken'])){
        $id_token = $_POST['idtoken'];
        $payload = $client->verifyIdToken($id_token);

        ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM