【问题标题】:Google always returns false verifying id token谷歌总是返回错误的验证 id 令牌
【发布时间】:2017-12-31 02:42:05
【问题描述】:

我有下一个代码,直接来自谷歌参考 (https://developers.google.com/identity/sign-in/web/backend-auth)

public function verifyFromAndroid($idToken=null) {
        if(empty($idToken)) {
            $idToken = self::SAMPLE_ID_TOKEN;
        }
        $client = new Google_Client(['client_id' => self::CLIENT_ID]);
        $payload = $client->verifyIdToken($idToken);
        if ($payload) {
            print_r($payload);
            $userid = $payload['sub'];
            // If request specified a G Suite domain:
            //$domain = $payload['hd'];
        } else {
            var_dump($payload);
            $this->lastError = "Invalid ID token";
            return false;
        }
    }

但此方法总是返回 false,即使使用使用 oauthplayground online tool 创建和工作的有效 id 令牌也是如此。

下一个代码可以正常工作,直接使用 GoogleAccessToken_Verify 类。谁能告诉我为什么官方的 Google 代码不起作用,是我自己的代码使用官方的 Google-clien-php sdk 吗?

try {
            $verify = new Google_AccessToken_Verify();
            $result = $verify->verifyIdToken($this->idToken);
            if($result) {

                print_r($result);
                $friendlyData = $this->translateData($result, true);
                if(!$friendlyData) {
                    return false;
                }
                return $friendlyData;
            }
            else {
                $this->lastError = "Invalid token verification, no error code";
                return false;
            }
        }
        catch(UnexpectedValueException $ex) {
            $this->lastError = "UnVaEx (Code {$ex->getCode()}): {$ex->getMessage()}";
            return false;
        }

【问题讨论】:

  • 你是怎么解决这个问题的?
  • 没有解决办法

标签: php google-signin


【解决方案1】:

尝试添加完整的客户端 ID

xxxxxxxxxxxxxx-xxxxx-yy-zz.apps.googleusercontent.com

在启动时

$client = new Google_Client(['client_id' => self::CLIENT_ID]);

它应该工作我也面临同样的问题......

【讨论】:

  • 是的,我错过了.apps.googleusercontent.com,添加它后,它工作正常。谢谢你。但我想知道客户端密钥,我能用它做什么,或者只是为了好玩:v
  • 它用于服务器端验证您收到的成功验证令牌
【解决方案2】:

有一个类似的问题。在 firebase 控制台上删除了我的 android 应用程序并创建了一个新的应用程序 wirh 调试密钥 sha1。然后下载并将我的 google.json 替换到我的应用程序中。这解决了我的问题。这已经发生在我身上两次了。有时您只需要在 firebase 控制台上重新创建 android 应用即可。

【讨论】:

    【解决方案3】:

    在您开始使用 配置您的项目 按钮在https://developers.google.com/identity/sign-in/web/sign-in 注册您的后端 URL 之前,不要在您的代码中使用任何凭据或 api 密钥。完成这些操作后,您的代码应如下所示。

    public function verifyFromAndroid($idToken=null) {
        if(empty($idToken)) {
            $idToken = self::SAMPLE_ID_TOKEN;
        }
        //As you notice we don't use any key as a parameters in Google_Client() API function
        $client = new Google_Client();
        $payload = $client->verifyIdToken($idToken);
        if ($payload) {
            print_r($payload);
            $userid = $payload['sub'];
            // If request specified a G Suite domain:
            //$domain = $payload['hd'];
        } else {
            var_dump($payload);
            $this->lastError = "Invalid ID token";
            return false;
        }
    }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-07-20
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-18
      • 2017-05-19
      • 2016-12-10
      相关资源
      最近更新 更多