Google Plus API身份验证用户

时间:2014-06-13 08:24:28

标签: php google-plus google-api-php-client

我使用以下代码对用户进行身份验证(它基于github上的服务帐户的googles示例):

<?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>";
  }

我正在尝试获取用户帐户103283326808099970387的信息,但有些我如何通过用户111631880133159328429进行身份验证。

如果我转到https://plus.google.com/111631880133159328429,我会收到404错误。

0 个答案:

没有答案