google 身份验证令牌过期

google authentication expired token

我需要在 javascript google 登录后恢复用户信息。 身份验证工作正常,我将 googleUser.Ka 对象发送到我的 php 那里有这个代码

$api = new Google_Client();
$api->setApplicationName($this->_configCustomer['google']['api']['application_name']);
$api->setClientId($this->_configCustomer['google']['api']['client_id']); // Set Client ID
$api->setClientSecret($this->_configCustomer['google']['api']['client_secret']); //Set client Secret
$api->setAccessType('online'); // Access method
$api->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
//$api->setRedirectUri('http://www.infotuts.com/demo/googlelogin/login.php');
$service = new Google_Service_Plus($api);
$oauth2 = new \Google_Service_Oauth2($api);
$api->setAccessToken(json_encode($_POST));

//ADDED AFTER ERROR
if($api->isAccessTokenExpired()) {
    $api->authenticate();
    $NewAccessToken = json_decode($api->getAccessToken());
    $api->refreshToken($NewAccessToken->refresh_token);
}
//END ADDED AFTER FIRST ERROR


//$api->authenticate($_POST['access_token']);
if ($api->getAccessToken()) {
    $data = $service->people->get('me');
    $user_data = $oauth2->userinfo->get();
    var_dump($user_data);
    die();
}

此代码生成错误:

The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved.

我在评论中添加了代码指示link

OAuth 2.0 access token has expired, and a refresh token is not available

但在最新版本的 php 中,身份验证方法需要 $code 参数。这是什么?

谢谢

我是这样解决的

$api = new Google_Client();
$api->setApplicationName($this->_configCustomer['google']['api']['application_name']);
$api->setClientId($this->_configCustomer['google']['api']['client_id']); // Set Client ID
$api->setClientSecret($this->_configCustomer['google']['api']['client_secret']); //Set client Secret
$api->setAccessType('online'); // Access method
$api->setScopes(array('https://www.googleapis.com/auth/plus.login', 'https://www.googleapis.com/auth/plus.me', 'https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/userinfo.profile'));
$api->setRedirectUri('postmessage');
$result = $api->verifyIdToken($_POST['id_token']);

我发送id_token,从ajax登录获取,并使用客户端库的verifyIdToken方法恢复用户信息