谷歌身份验证过期令牌

时间:2015-08-18 21:23:08

标签: oauth-2.0 google-oauth2

我需要在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.

我在评论中添加了代码,请点击此链接

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

但是在最后一个版本的php authenticate方法中需要$ code参数。它是什么?

由于

1 个答案:

答案 0 :(得分:0)

我以这种方式解决了

$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']);

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