无法使用 Google Oauth 获取刷新令牌

问题描述 投票:0回答:1

我正在使用 CURL 来获取访问令牌和刷新令牌。 现在我有两个功能:

    public function handle_google_oauth_request() {
        if (isset($_POST['authorize'])) {
            $auth_url = 'https://accounts.google.com/o/oauth2/auth?';
            $auth_url .= 'client_id=' . urlencode($this->client_id);
            $auth_url .= '&redirect_uri=' . urlencode($this->redirect_uri);
            $auth_url .= '&scope=' . urlencode($this->scope);
            $auth_url .= '&response_type=code'; 

            wp_redirect($auth_url);
            exit;
        }
    }

    public function handle_google_code() {
        if (isset($_GET['code'])) {
            $code = $_GET['code'];
            $url = 'https://oauth2.googleapis.com/token';

            $params = array(
                'code' => $code,
                'client_id' => $this->client_id,
                'client_secret' => $this->client_secret,
                'redirect_uri' => $this->redirect_uri,
                'grant_type' => 'authorization_code',
            );

            $params_string = http_build_query($params);

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/x-www-form-urlencoded'
            ));

            $response = curl_exec($ch);
            curl_close($ch);

            if ($response === false) {
                echo 'cURL error: ' . curl_error($ch);
            } else {
                $token_data = json_decode($response, true);

                if (isset($token_data['access_token'])) {
                    $access_token = $token_data['access_token'];
                    update_option('google_access_token', $access_token);
                }
            }
        }
    }

结果 - 我可以从我的handle_google_code函数中获取访问令牌。但我无法获取刷新令牌。

有人有解决这个问题的想法吗?

我知道我需要在 $auth_url 中添加 2 个参数:

$auth_url .= '&prompt=consent';
$auth_url .= '&access_type=offline';

但结果 - 我收到错误 invalid_grant Bad Request。

我还尝试通过从帐户访问权限中删除我的应用程序并再次获取令牌来获取刷新令牌,这也没有帮助。

php wordpress google-calendar-api google-workspace
1个回答
0
投票

我猜测您创建的客户端类型是 Web 客户端。 Web 客户端仅在用户第一次授权时返回刷新令牌。 Google 希望您存储它。

转到第三方应用程序下的用户帐户并删除访问权限,然后它会要求您再次授权。

或者您可以获取您拥有的访问令牌并向撤销端点发送请求,这也将删除访问权限。

然后再试一次,您应该会获得一个新的刷新令牌。

离线访问

我也认为你缺少access_type

 $auth_url = 'https://accounts.google.com/o/oauth2/auth?';
        $auth_url .= 'client_id=' . urlencode($this->client_id);
        $auth_url .= '&redirect_uri=' . urlencode($this->redirect_uri);
        $auth_url .= '&scope=' . urlencode($this->scope);
        $auth_url .= '&response_type=code'; 

如果您添加 access_type=offline 您应该会获得刷新令牌

参见网络服务器#创建客户端

© www.soinside.com 2019 - 2024. All rights reserved.