Php Google Plus API认证用户
我使用了以下代码对用户进行身份验证(它基于Google在github上的服务帐户示例):Php Google Plus API认证用户,php,google-plus,google-api-php-client,Php,Google Plus,Google Api Php Client,我使用了以下代码对用户进行身份验证(它基于Google在github上的服务帐户示例): 您已作为服务帐户进行身份验证。你试过了吗$数据=$info->people->get('103283368080809970387');但是,如果我再向前一步,尝试$data=$info->people listPeople('1032833680809970387','visible');,我就会得到这些信息;。我得到一个错误,因为我只能为经过身份验证的用户$data=$info->people list
您已作为服务帐户进行身份验证。你试过了吗$数据=$info->people->get('103283368080809970387');但是,如果我再向前一步,尝试$data=$info->people listPeople('1032833680809970387','visible');,我就会得到这些信息;。我得到一个错误,因为我只能为经过身份验证的用户$data=$info->people listPeople('me','visible')获取此信息;为什么你使用的是服务帐户而不是Oauth2?我不想让用户登录我只想使用我的帐户并使用服务器到服务器的控制服务帐户不是你。您不会看到您的帐户。
<?php
//API INFORMATION
$client_id='xx-x.apps.googleusercontent.com';
$client_email='xx-xx@developer.gserviceaccount.com';
$client_key='xx';
$cert_loc='directory/google-api-bushbase-privatekey.p12';
//SET THE APP SCROPES
$scopes=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'
);
//GET ASSERTION CREDENTIALS
if(($key=file_get_contents($cert_loc))!==false){
//GOT GOOGLE CERTIFICATE
try{
$auth= new Google_Auth_AssertionCredentials($client_email,$scopes,$key);
}catch (Exception $e){
echo $error;
echo 'Unable to get authenticate with Google servers</div>';
//RETURN FALSE
return false;
}
}else{
echo $error;
echo 'Unable to get Google certificate</div>';
//RETURN FALSE
return false;
}
//GOOGLE CONNECTION OBJECT
try {
$connection = new Google_Client();
$connection->setApplicationName("Bush Base");
$connection->setAssertionCredentials($auth);
//CHECK TO SEE IF TOKEN IS EXPIRED VIA SESSION
if (isset($_SESSION['service_token'])) {
$connection->setAccessToken($_SESSION['service_token']);
}
if($connection->getAuth()->isAccessTokenExpired()){
//TOKEN IS EXPIRED SO RESET WITH ASSERTION CRED
$connection->getAuth()->refreshTokenWithAssertion($auth);
}
//GET THE TOKEN VALUE
$_SESSION['service_token']=$connection->getAccessToken();
$connection->setAccessToken($_SESSION['service_token']);
//THE CONNECTION WAS NOT SUCCESSFUL
}catch (Exception $e){
echo $error;
echo 'Unable to connect with Google database</div>';
//RETURN FALSE
return false;
}
//CREATE THE NEW GOOGLE SERVICES OBJECT
try{
$info =new Google_Service_Plus($connection);
}catch (Exception $e){
echo $error;
echo 'Unable to conntect to google services</div>';
//RETURN FALSE
return false;
}
try{
//GET THE USER INFO
$data= $info->people->get('me');
echo "<pre>";
print_r($data);
echo "</pre>";
}