2014-12-24 7 views
0

Google에서 내 웹 페이지로 연락처를 가져 오려고합니다. Google 계정에서 연락처의 이메일 주소를 받아야합니다. 나는 this link을 언급하고있다. 이 코드는 리디렉션 URL 페이지에 연락처의 이메일 주소를 나열합니다.PHP로 Google 연락처 가져 오기

예상되는 이메일 주소를 표시 할 것으로 예상되지만, 예상 한 결과가 나타나지 않습니다. 나는 비어있게된다. 내가 실수 한 곳을 모른다.

oauth.php

  <html> 
      <head> 
      <meta name="robots" content="noindex" /> 
      <title>Import Gmail or Google contacts using Google Contacts Data API and OAuth 2.0</title> 
      <style type="text/css"> 
      a:link {color:Chocolate;text-decoration: none;} 
      a:hover {color:CornflowerBlue;} 
      .logo{width:100%;height:110px;border:2px solid black;background-color:#666666;} 
      </style> 
      </head> 
      <body> 

      <br/> 
      <br/><br/> 
      <div align="center" > 
      <a style="font-size:25px;font-weight:bold;" href="https://accounts.google.com/o/oauth2/auth?client_id=my_client_id&redirect_uri=my_redirect_uri&scope=https://www.google.com/m8/feeds/&response_type=code">Click here to Import Gmail Contacts</a> 
      </div> 
      </body> 
      </html> 

사람이 내가 잘못 한 곳에 나를 찾는 데 도움이 될 수

  <html> 
      <head> 
      <meta name="robots" content="noindex" /> 
      <title>Email address list - Import Google contacts</title> 
      <style type="text/css"> 
      a:link {color:Chocolate;text-decoration: none;} 
      a:hover {color:CornflowerBlue;} 
      .logo{width:100%;height:110px;border:2px solid black;background-color:#666666;} 
      </style> 
      </head> 
      <body> 
      <br/> 
      <br/> 
      <div style="padding-left: 50px;"> 
      <?php 
      $client_id='my_client_id'; 
      $client_secret='my_client_secret'; 
      $redirect_uri='my_redirect_url'; 
      $max_results = 25; 

      $auth_code = $_GET["code"]; 

      function curl_file_get_contents($url) 
      { 
      $curl = curl_init(); 
      $userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)'; 

      curl_setopt($curl,CURLOPT_URL,$url); //The URL to fetch. This can also be set when initializing a session with curl_init(). 
      curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE); //TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly. 
      curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5); //The number of seconds to wait while trying to connect.  

      curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); //The contents of the "User-Agent: " header to be used in a HTTP request. 
      curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); //To follow any "Location: " header that the server sends as part of the HTTP header. 
      curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); //To automatically set the Referer: field in requests where it follows a Location: redirect. 
      curl_setopt($curl, CURLOPT_TIMEOUT, 10); //The maximum number of seconds to allow cURL functions to execute. 
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); //To stop cURL from verifying the peer's certificate. 

      $contents = curl_exec($curl); 
      curl_close($curl); 
      return $contents; 
      } 

      $fields=array(
      'code'=> urlencode($auth_code), 
      'client_id'=> urlencode($client_id), 
      'client_secret'=> urlencode($client_secret), 
      'redirect_uri'=> urlencode($redirect_uri), 
      'grant_type'=> urlencode('authorization_code') 
      ); 
      $post = ''; 
      foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; } 
      $post = rtrim($post,'&'); 

      $curl = curl_init(); 
      curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token'); 
      curl_setopt($curl,CURLOPT_POST,5); 
      curl_setopt($curl,CURLOPT_POSTFIELDS,$post); 
      curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE); 
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,FALSE); 
      $result = curl_exec($curl); 
      curl_close($curl); 

      $response = json_decode($result); 
      $accesstoken = $response->access_token; 

      $url = 'https://www.google.com/m8/feeds/contacts/default/full?max-results='.$max_results.'&oauth_token='.$accesstoken; 
      $xmlresponse = curl_file_get_contents($url); 
      if((strlen(stristr($xmlresponse,'Authorization required'))>0) && (strlen(stristr($xmlresponse,'Error '))>0)) //At times you get Authorization error from Google. 
      { 
      echo "<h2>OOPS !! Something went wrong. Please try reloading the page.</h2>"; 
      exit(); 
      } 
      echo "<h3>Email Addresses:</h3>"; 
      $xml = new SimpleXMLElement($xmlresponse); 
      $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005'); 
      $result = $xml->xpath('//gd:email'); 

      foreach ($result as $title) { 
      echo $title->attributes()->address . "<br>"; 
      } 
      ?> 
      </div> 
      </body></html> 

의 index.php :

여기 내 코드입니다.

+0

어떤 결과가 나타 납니까? – Victor2748

+0

이메일 주소를 표시 할 것으로 예상되지만 비워 둡니다. – Aishwaryas

+1

나는 구글이 나와 관계가없는 당신이나 제 3 자와 나의 연락처의 이메일 주소를 공유하지 않기를 바란다 ... –

답변

1

Google 연락처를 가져 오기 전에 개발자 콘솔에서 주소록 API를 켜야합니다.

여기에서 사용한 코드는 정상입니다. 내 개발자 콘솔의 주소록 API를 켰을 때 Google 연락처의 이메일 주소가 있습니다.