2009-11-10 109 views
10

我是OAuth新手,想要创建一个页面,使用OAuth系统从Google获取用户的联系人列表,以便他们不必登录。如何使用Google OAuth获取Google通讯录信息?

我该怎么做?我使用的是PHP,所以如果有这样的代码示例,我将非常感激。我似乎无法在Google上找到它。

请帮忙!

感谢

+0

此外,是否有办法雅虎地址簿,等等这样做......有帮助的呢? – chris 2009-11-10 08:42:21

回答

11

对于访问Google的一般OAuth原则,您可能会发现Google's OAuth playground非常有用(联系人包含在那里)。

这是一个非常基本的例子(使用PHP的OAuth PECL扩展和SimpleXML,它只是打印出25个联系人的姓名):

<?php 
$cons_key="your consumer key"; 
$cons_sec="your consumer secret"; 

$callback="http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; 

$req_token_url="https://www.google.com/accounts/OAuthGetRequestToken"; 
$auth_token_url="https://www.google.com/accounts/OAuthAuthorizeToken"; 
$acc_token_url="https://www.google.com/accounts/OAuthGetAccessToken"; 

$scope="https://www.google.com/m8/feeds/"; 
$scopes=urlencode($scope); 
$req_scope_token_url=$req_token_url."?scope=".$scopes; 
$endpoint="https://www.google.com/m8/feeds/contacts/default/full/"; 

session_start(); 

if(!isset($_GET['oauth_token']) && $_SESSION['state']==1) $_SESSION['state'] = 0; 

try { 
    $oauth = new OAuth($cons_key,$cons_sec); 
    if(!isset($_GET['oauth_token']) && !$_SESSION['state']) { 
     $oauth = new OAuth($cons_key,$cons_sec); 
     $oauth->setRequestEngine(OAUTH_REQENGINE_CURL); 
     $request_token_info = $oauth->getRequestToken($req_scope_token_url,$callback); 
     if(!empty($request_token_info)) { 
      $_SESSION['token']=$request_token_info['oauth_token']; 
      $_SESSION['secret']=$request_token_info['oauth_token_secret']; 
      $_SESSION['state']=1; 
      header('Location: '.$auth_token_url.'?oauth_token='.$_SESSION['token']); 
      exit; 
     } 
    } else if($_SESSION['state']==1) { 
     $oauth->setToken($_GET['oauth_token'],$_SESSION['secret']); 
     $access_token_info = $oauth->getAccessToken($acc_token_url); 
     $_SESSION['state'] = 2; 
     $_SESSION['token'] = $access_token_info['oauth_token']; 
     $_SESSION['secret'] = $access_token_info['oauth_token_secret']; 
    } 

    $oauth->fetch($endpoint); 
    parseAtom($oauth->getLastResponse()); 

} catch(OAuthException $E) { 
    print_r($E); 
} 

function parseAtom($atomstring) { 
    global $oauth; 
    $atom=simplexml_load_string($atomstring); 
    foreach ($atom->entry as $entry) { 
     print $entry->title.", "; 
    } 
} 
?> 

你可以看到上面的代码in action here

安装(配置)oauth pecl扩展可能会很棘手,您may have to check your php.ini and/or specify a requestengine

+0

嗯,没有看到这实际上是一个老问题,最近的回复:) – futtta 2010-08-24 09:38:22

+1

似乎有一个小错误 - 获取访问令牌后,代码首先使用SESSION变量设置令牌,然后才分配它们。我认为它应该是反向的--setToken应该在分配之后进行。 – StasM 2010-10-27 21:35:35

+0

绝对stasM;我错误地复制/粘贴了“$ oauth-> setToken($ _ SESSION ['token'],$ _ SESSION ['secret']);”在那里2次,感谢您的发现! – futtta 2010-11-03 14:05:45

1

好的。首先,我有一些pecl和梨的问题。所以我决定坚持使用ZEND。我很感激你发布虽然,你的代码仍然帮助我: - )....

这是我迄今为止,从上面张贴的ibm页面的一些代码,但改变它使用OAuth的才能进去。

<?php 

session_start(); 

require_once 'common.php'; 
require_once 'Zend/Oauth/Consumer.php'; 
require_once 'Zend/Crypt/Rsa/Key/Private.php'; 

require_once 'Zend/Gdata.php'; 
require_once 'Zend/Gdata/Query.php'; 




$oauthOptions = array(
    'requestScheme'  => Zend_Oauth::REQUEST_SCHEME_HEADER, 
    'version'    => '1.0', 
    'consumerKey'   => 'PUT KEY HERE', 
    'consumerSecret'  => 'PUT KEY HERE', 
    'signatureMethod'  => 'HMAC-SHA1', 
    'requestTokenUrl'  => 'https://www.google.com/accounts/OAuthGetRequestToken', 
    'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken', 
    'accessTokenUrl'  => 'https://www.google.com/accounts/OAuthGetAccessToken' 
); 


?> 


<!DOCTYPE html 
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
    <head> 
    <title>Listing contacts</title> 
    <style> 
    body { 
     font-family: Verdana;  
    } 
    div.name { 
     color: red; 
     text-decoration: none; 
     font-weight: bolder; 
    } 
    div.entry { 
     display: inline; 
     float: left; 
     width: 400px; 
     height: 150px; 
     border: 2px solid; 
     margin: 10px; 
     padding: 5px; 
    } 
    td { 
     vertical-align: top; 
    } 
    </style>  
    </head> 
    <body> 

    <? 



$consumer = new Zend_Oauth_Consumer($oauthOptions); 

if (!isset($_SESSION['ACCESS_TOKEN_GOOGLE'])) { 
    if (!empty($_GET)) { 
     $token = $consumer->getAccessToken($_GET, unserialize($_SESSION['REQUEST_TOKEN_GOOGLE'])); 
     $_SESSION['ACCESS_TOKEN_GOOGLE'] = serialize($token); 
    } else { 
     $token = $consumer->getRequestToken(array('scope'=>'http://www.google.com/m8/feeds')); 
     $_SESSION['REQUEST_TOKEN_GOOGLE'] = serialize($token); 
     $consumer->redirect(); 
     exit; 
    } 
} else { 
    $token = unserialize($_SESSION['ACCESS_TOKEN_GOOGLE']); 
    //$_SESSION['ACCESS_TOKEN_GOOGLE'] = null; 
} 



$http = $token->getHttpClient($oauthOptions); 
$gdata = new Zend_Gdata($http); 
$gdata->setMajorProtocolVersion(3); 

$gdata->getHttpClient()->setRequestScheme(Zend_Oauth::REQUEST_SCHEME_QUERYSTRING); 


$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full?); 
//$query->setMaxResults(10); 
$feed = $gdata->getFeed($query); 

?> 



     <h2><?php echo $feed->title; ?></h2> 
     <div> 
     <?php echo $feed->totalResults; ?> contact(s) found. 
     </div> 

     <?php 
     // parse feed and extract contact information 
     // into simpler objects 
     $results = array(); 
     foreach($feed as $entry){ 
     $xml = simplexml_load_string($entry->getXML()); 
     $obj = new stdClass; 
     $obj->name = (string) $entry->title; 
     $obj->orgName = (string) $xml->organization->orgName; 
     $obj->orgTitle = (string) $xml->organization->orgTitle; 

     foreach ($xml->email as $e) { 
      $obj->emailAddress[] = (string) $e['address']; 
     } 

     foreach ($xml->phoneNumber as $p) { 
      $obj->phoneNumber[] = (string) $p; 
     } 
     foreach ($xml->website as $w) { 
      $obj->website[] = (string) $w['href']; 
     } 

     $results[] = $obj; 
     } 


    ?> 

    <?php 
    // display results 
    foreach ($results as $r) { 
    ?> 
    <div class="entry"> 
     <div class="name"><?php echo (!empty($r->name)) ? 
     $r->name : 'Name not available'; ?></div> 
     <div class="data"> 
     <table> 
      <tr> 
      <td>Organization</td> 
      <td><?php echo $r->orgName; ?></td> 
      </tr> 
      <tr> 
      <td>Email</td> 
      <td><?php echo @join(', ', $r->emailAddress); ?></td> 
      </tr> 
      <tr> 
      <td>Phone</td> 
      <td><?php echo @join(', ', $r->phoneNumber); ?></td> 
      </tr> 
      <tr> 
      <td>Web</td> 
      <td><?php echo @join(', ', $r->website); ?></td> 
      </tr> 
     </table> 
     </div> 
    </div> 
    <?php 
    } 
    ?> 

    </body> 
</html> 

还存在一些问题,对我来说唯一的支柱25分的结果,我似乎无法得到setmaxresults工作...似乎有与Zend问题为...如果有人知道周围的工作,请张贴。

b

1

注释掉setRequestScheme方法调用,并使用最大结果,你应该能够获得超过25项。

//$gdata->getHttpClient()->setRequestScheme(Zend_Oauth::REQUEST_SCHEME_QUERYSTRING); 

$query = new Zend_Gdata_Query('http://www.google.com/m8/feeds/contacts/default/full/?max-results=999999'); 
相关问题