【问题标题】:Revoke Token OAuth 2 google api撤销令牌 OAuth 2 google api
【发布时间】:2017-06-20 02:54:20
【问题描述】:

我想撤销 Token 以使用更长时间,我的代码以标准重定向开头 -> getToken 流程:

        $code = (array_key_exists('code', $_GET) ? $_GET['code'] : '');
        $access_token = json_decode(DB::getToken(), true);

        $guzzleClient = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, ), ));
        $this->client = new Google_Client();
        $this->client->setHttpClient($guzzleClient);
        $this->client->setAuthConfig('client_secret.json');
        $this->client->addScope("https://www.googleapis.com/auth/calendar");
        $this->client->setAccessType('offline');
        $this->client->setApprovalPrompt('force');
        $this->client->setAccessToken($access_token);

        if(!$code && !$access_token) {
            $auth_url = $this->client->createAuthUrl();
            header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
        }

        if($code && !$access_token) {
            $this->client->fetchAccessTokenWithAuthCode($code);
            $access_token = $this->client->getAccessToken();
            DB::saveToken($access_token);
        }

在这里我检查访问令牌是否已过期,如果是,我会撤销令牌并希望继续使用它而不会出现问题:

        if($this->client->isAccessTokenExpired()) {
            $revoked = $this->client->revokeToken($access_token);
            if($revoked) {
                $access_token = $this->client->getAccessToken();
            }
        }

虽然出现问题并出现错误,但我收到 Token expired or revoked 错误消息。

我在这里做错了什么?

我希望在撤销后,令牌再次有效。

【问题讨论】:

    标签: php oauth oauth-2.0 google-oauth


    【解决方案1】:

    我希望在撤销后,令牌再次有效。

    不能再使用过期或撤销的访问令牌。撤销过期的访问令牌也没有用..

    我在您的代码中看到您要求提供offline 访问令牌。您应该会收到一个刷新令牌。 您要做的是使用该刷新令牌来获取新的访问令牌。 见that post for more information

    【讨论】:

    • 你拯救了我的一天。谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多