2013-05-03 57 views
0
<?php 
/* 
* Copyright 2011 Google Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
require_once '/google-api-php-client/src/Google_Client.php'; 
require_once '/google-api-php-client/src/contrib/Google_PlusService.php'; 
require_once '/google-api-php-client/src/contrib/Google_Oauth2Service.php'; 
require_once '/google-api-php-client/src/io/Google_HttpRequest.php'; 
session_start(); 

$client = new Google_Client(); 
// Visit https://code.google.com/apis/console to generate your 
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri. 
$client->setClientId('123236981117-cr7rt023bpg3n9b698pdqhk0353hpjgi.apps.googleusercontent.com'); 
$client->setClientSecret(''); 
$client->setRedirectUri(''); 
$client->setDeveloperKey(''); 


$plus = new Google_PlusService($client); 
$oauth2 = new Google_Oauth2Service($client); 

if (isset($_REQUEST['logout'])) { 
    unset($_SESSION['access_token']); 
} 

if (isset($_GET['code'])) { 

// Exchange the OAuth 2.0 authorization code for user credentials. 

    $client->authenticate($_GET['code']); 
    $token = json_decode($client->getAccessToken()); 
// Verify the token 

    $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=' . 
      $token->access_token; 
    $req = new Google_HttpRequest($reqUrl); 


    $tokenInfo = json_decode(
     $client::getIo()->authenticatedRequest($req)->getResponseBody()); 

    $_SESSION['access_token'] = $client->getAccessToken(); 

    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); 
} 

if (isset($_SESSION['access_token'])) { 
    $client->setAccessToken($_SESSION['access_token']); 
} 

if ($client->getAccessToken()) { 
    $me = $plus->people->get('me'); 
    $user = $oauth2->userinfo->get(); 

     // These fields are currently filtered through the PHP sanitize filters. 
    // See http://www.php.net/manual/en/filter.filters.sanitize.php 


    $url = filter_var($me['url'], FILTER_VALIDATE_URL); 

    $birth = filter_var($user['birthday'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 

    $img = filter_var($me['image']['url'], FILTER_VALIDATE_URL); 

    $name = filter_var($me['displayName'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 

    $personMarkup = "<a rel='me' href='$url'>$name</a><div><img src='$img'></div>"; 

    $optParams = array('maxResults' => 100); 
    $activities = $plus->activities->listActivities('me', 'public', $optParams); 


    $activityMarkup = ''; 
    foreach($activities['items'] as $activity) { 
    // These fields are currently filtered through the PHP sanitize filters. 
    // See http://www.php.net/manual/en/filter.filters.sanitize.php 
    $url = filter_var($activity['url'], FILTER_VALIDATE_URL); 
    $title = filter_var($activity['title'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 
    $content = filter_var($activity['object']['content'], FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH); 

    $activityMarkup .= "<div class='activity'><a href='$url'>$title</a><div>$content</div></div>"; 
    } 


    // The access token may have been updated lazily. 
    $_SESSION['access_token'] = $client->getAccessToken(); 
} else { 
    $authUrl = $client->createAuthUrl(); 
} 
?> 
<!doctype html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <link rel='stylesheet' href='style.css' /> 
</head> 
<body> 
<header><h1>Google+ Sample App</h1></header> 
<div class="box"> 

<?php if(isset($personMarkup)): ?> 
<div class="me"><?php print $personMarkup ?></div> 
<?php endif ?> 

<?php if(isset($activityMarkup)): ?> 
<div class="activities">Your Activities: <?php print $activityMarkup ?></div> 
<?php endif ?> 

<?php 
    if(isset($authUrl)) { 
    print "<a class='login' href='$authUrl'>Connect Me!</a>"; 
    } else { 
    print "<a class='logout' href='?logout'>Logout</a>"; 
    } 
?> 
</div> 
</body> 
</html> 

嘿每一个通过使用此代码我得到用户的信息和用户的activites。 我也正在验证令牌。但问题是我不知道如何获得谷歌+用户ID如果认证码给我。所以我想做以下事情在谷歌加如何获得userId,而验证码给出和谷歌加注册按钮(PHP)

如果在令牌信息中出现错误,中止。

我想添加谷歌加注册按钮也进入我的appliaction。

请有人帮我。谢谢

回答

1

用户的用户ID将是$ me中返回的元素之一。您可以将它与客户端获取(并传递给服务器)的UserID进行比较,以验证客户端是否试图对服务器说谎。