本文介紹了從 Google OAuth 2.0 PHP API 獲取用戶信息的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我正在嘗試使用 Google Oauth API 來獲取用戶信息.它非常適合 Google Plus API,但我正在嘗試創(chuàng)建備份,以防用戶沒有 google plus 帳戶.身份驗證過程是正確的,我什至獲得了 $userinfo 對象,但我究竟如何訪問這些屬性.我試過 $userinfo->get() 但它只返回用戶的 id.
我做錯了嗎?這是我正在使用的代碼...
require_once '../../src/Google_Client.php';require_once '../../src/contrib/Google_Oauth2Service.php';session_start();$client = new Google_Client();$client->setApplicationName("Google+ PHP Starter Application");//訪問 https://code.google.com/apis/console 生成您的//oauth2_client_id、oauth2_client_secret,并注冊您的 oauth2_redirect_uri.$client->setClientId('*********************');$client->setClientSecret('****************');$client->setRedirectUri('***************');$client->setDeveloperKey('**************');$plus = new Google_Oauth2Service($client);如果(isset($_REQUEST ['注銷'])){未設(shè)置($_SESSION['access_token']);}如果(isset($_GET['代碼'])){$client->authenticate($_GET['code']);$_SESSION['access_token'] = $client->getAccessToken();header('位置:http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);}如果(isset($_SESSION['access_token'])){$client->setAccessToken($_SESSION['access_token']);}if ($client->getAccessToken()){$userinfo = $plus->userinfo;print_r($userinfo->get());} 別的{$authUrl = $client->createAuthUrl();}?><!doctype html><頭><meta charset="utf-8"><link rel='stylesheet' href='style.css'/>頭部><身體><header><h1>Google+ 示例應(yīng)用</h1></header><div class="box"><?php if(isset($personMarkup)): ?><div class="me"><?php print $personMarkup ?></div><?php endif ?><?php如果(isset($authUrl)){print "<a class='login' href='$authUrl'>連接我!</a>";} 別的 {打印 "<a class='logout' href='?logout'>Logout</a>";}?>