2013-04-30 8 views
2
<?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'; 

session_start(); 

$client = new Google_Client(); 
$client->setApplicationName("Google+ PHP Starter Application"); 
// 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(''); 
$client->setClientSecret(''); 
$client->setRedirectUri(''); 
$client->setDeveloperKey(''); 

$plus = new Google_PlusService($client); 

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

if (isset($_GET['code'])) { 
    $client->authenticate($_GET['code']); 
    $_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'); 
    var_dump($me); 

    // 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); 

    $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); 
    var_dump($content); 
    exit; 
    $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> 

我無法獲取用戶的電子郵件ID或生日。請提供建議以獲取用戶的電子郵件ID和生日。如何在Google +中獲取用戶的電子郵件和生日

+0

你得到什麼錯誤? – Slim 2013-04-30 12:52:30

+0

當我添加行$ birth = filter_var($ me ['birthday'],FILTER_SANITIZE_STRING,FILTER_FLAG_STRIP_HIGH);在代碼中我得到了錯誤通知:未定義的指數:生日在E:\ wamp \ www \ google \ code.php上線56 – user2330998 2013-04-30 12:54:24

回答

1

您的示例中有幾處遺漏信息可能會影響您獲得的結果。您可能希望從https://github.com/googleplus/gplus-quickstart-php的快速入門應用程序開始,並專注於index.html中的登錄按鈕配置和signin.php中的oauth配置。

特別是,您需要確保您在index.html頁面中請求您需要的oauth範圍。您上面的示例中沒有顯示此部分,但要獲取生日信息(假設用戶已設置),則需要請求https://www.googleapis.com/auth/plus.login範圍,並且需要訪問其電子郵件地址訪問https://www.googleapis.com/auth/userinfo.email,然後從「tokeninfo」端點請求信息。有關更多信息,請參閱https://developers.google.com/+/api/oauth

您顯示的代碼示例還顯示了activities.get,而不是people.get方法。您可能需要發佈說明確切問題的代碼。但是,在這種情況下,請記住,如果該人未公開其生日,則不會被授予訪問此字段的權限。

+0

嘿在給定的代碼中,我可以使用$ client-> setScopes('https:// www .googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.email'); – user2330998 2013-04-30 13:46:51

+0

我不得不瀏覽源代碼,因爲我不熟悉PHP庫,但看起來您可以像這樣指定範圍,是的。我更習慣於擁有登錄頁面並使用登錄按鈕來執行初始身份驗證/授權(我通過快速入門指向的URL)。 – Prisoner 2013-04-30 14:02:43

+0

是的,我們可以setScopes,但問題是當試圖添加$ email = filter_var($ me ['email'],FILTER_SANITIZE_EMAIL);在上面的代碼中給出錯誤注意:未定義的索引:電子郵件地址E:\ wamp \ www \ google \ code.php在第56行 – user2330998 2013-04-30 14:07:22

相關問題