2009-11-10 11 views
10

मैं ओएथ के लिए नया हूं, और एक ऐसा पृष्ठ बनाना चाहता हूं जो ओएथ सिस्टम का उपयोग कर Google से उपयोगकर्ता की संपर्क सूची प्राप्त करे, इसलिए उन्हें लॉगिन करने की आवश्यकता नहीं है।Google OAuth का उपयोग करके Google संपर्क जानकारी कैसे प्राप्त करें?

मैं यह कैसे कर सकता हूं? मैं php का उपयोग कर रहा हूं, इसलिए अगर मैं उदाहरण कोड करता हूं तो मैं वास्तव में सराहना करता हूं। मुझे Google पर यह प्रतीत नहीं होता है।

कृपया मदद करें!

धन्यवाद

+0

इसके अलावा, वहाँ याहू पता पुस्तिकाओं, आदि के लिए यह करने के लिए तरीके हैं ... (PHP (गूगल OAuth) में Google संपर्क सूची पाठक) सभी के लिए उपयोगी? – chris

उत्तर

1

आप यह करने के बाद, this एक संदर्भ के लिए पर्याप्त होना चाहिए Google Contacts Data API और OAuth, के साथ शुरू करने की जरूरत है।

11

सामान्य ओएथ सिद्धांतों के लिए Google तक पहुंचने के लिए, आपको 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 देख सकते हैं।

ओथ pecl एक्सटेंशन को स्थापित करना (कॉन्फ़िगर करना) मुश्किल हो सकता है, आप may have to check your php.ini and/or specify a requestengine

+0

हम्म, यह वास्तव में एक हालिया उत्तर के साथ एक पुराना सवाल नहीं देखा था :) – futtta

+1

टोकन पहुंचने के बाद, एक छोटी सी बग प्रतीत होती है, कोड पहले टोकन सेट करने के लिए सत्र वर्र्स का उपयोग करता है और केवल तभी उन्हें असाइन करता है। मुझे लगता है कि यह रिवर्स में होना चाहिए - सेट टोकन असाइनमेंट के बाद जाना चाहिए। – StasM

+0

बिल्कुल stasM; मैं गलत प्रतिलिपि/चिपका हुआ था और "$ oauth-> सेट टोकन ($ _ सत्र ['टोकन'], $ _ सत्र ['गुप्त']) था;" वहां 2 बार, स्पॉटिंग के लिए धन्यवाद! – futtta

1

ठीक है। सबसे पहले सभी futtta मुझे pecl और नाशपाती सामान के साथ कुछ समस्याएं थीं। इसलिए मैंने ज़ेंड के साथ चिपकने का फैसला किया। मैं आपको पोस्ट करने की सराहना करता हूं, हालांकि, आपके कोड ने अभी भी मेरी मदद की: -) ....

यहां मुझे जो मिला है, वह ऊपर दिए गए 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 के साथ समस्याओं हो रहा है ... अगर कोई काम आसपास जानता है, कृपया पोस्ट करें।

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'); 
संबंधित मुद्दे