簡體   English   中英

撤銷令牌OAuth 2 Google API

[英]Revoke Token OAuth 2 google api

我想撤銷令牌以使用更長的時間,我的代碼從標准重定向-> 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錯誤消息。

我在這里做錯了什么?

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

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

過期或已撤銷的訪問令牌無法再使用。 吊銷過期的訪問令牌也無濟於事。

我在您的代碼中看到您要求提供offline訪問令牌。 您應該會收到刷新令牌。 您要做的就是使用該刷新令牌來獲取新的訪問令牌。 有關更多信息,請參見該帖子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM