从Oauth和php导入gmail联系人列表时出现问题

从Oauth和php导入gmail联系人列表时出现问题,php,email,oauth,gmail,Php,Email,Oauth,Gmail,我正在尝试导入当前注册用户的gmail联系人列表 我的index.php是这样的- <html> <head> <meta name="robots" content="noindex" /> <title>Import Gmail or Google contacts</title> <style type="text/css"> a:link {color:Chocolate;text-decor

我正在尝试导入当前注册用户的gmail联系人列表

我的index.php是这样的-

<html>
<head>
    <meta name="robots" content="noindex" />
    <title>Import Gmail or 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/>
    <div align="center" >
        <a  style="font-size:25px;font-weight:bold;" href="https://accounts.google.com/o/oauth2/auth?client_id=969153863001-sfc4igc17cgqjqoifafqmjjbr6bouha3.apps.googleusercontent.com&redirect_uri=http://localhost/import_gmail/oauth.php&scope=https://www.google.com/m8/feeds/&response_type=code">Click here to Import Gmail Contacts</a>
    </div>
</body>

导入Gmail或谷歌联系人
链接{颜色:巧克力;文本装饰:无;}
a:悬停{颜色:矢车菊蓝;}
.logo{宽度:100%;高度:110像素;边框:2倍纯黑;背景色:#666666;}

而oauth.php就像-

<?php
$client_id = '969153863001-sfc4igc17coifafqmjjbr6bouha3.apps.googleusercontent.com';
$client_secret = 'TCMNd2UMBGUESGLoPgWcOOjh2bNL';
$redirect_uri = 'http://wwww.mysite.com/import_gmail_contacts/oauth.php';
$max_results = 100;

$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);   
 curl_setopt($curl,CURLOPT_RETURNTRANSFER,TRUE);    
 curl_setopt($curl,CURLOPT_CONNECTTIMEOUT,5);   

 curl_setopt($curl, CURLOPT_USERAGENT, $userAgent); 
 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);  
 curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE); 
 curl_setopt($curl, CURLOPT_TIMEOUT, 10);   
 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);

 $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,0);
 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);
 $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))
{
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>";
  }
  ?>
0)&(strlen(stristr($xmlresponse,'Error'))>0)
{
echo“哎呀!!出现问题。请尝试重新加载页面。”;
退出();
}
回声“电子邮件地址:”;
$xml=新的SimpleXMLElement($xmlresponse);
$xml->registerXPathNamespace('gd','http://schemas.google.com/g/2005');
$result=$xml->xpath('//gd:email');
foreach($result作为$title){
echo$title->attributes()->地址。“
”; } ?>
现在的问题是,我可以为登录的特定用户导入联系人列表。它正在导入电子邮件id的联系人列表,该电子邮件id是从我的浏览器登录的,我的电子邮件id为

那么,如何更新这段代码,在这里我可以根据需要显式地获取联系人列表


谢谢

我想你的url必须是:

$url = 'https://www.google.com/m8/feeds/contacts/{otherUsersEmail}/full?max-   results='.$max_results.'&oauth_token='.$accesstoken
而不是

$url = 'https://www.google.com/m8/feeds/contacts/default/full?max-   results='.$max_results.'&oauth_token='.$accesstoken
因为如果它是默认的,api将使用客户id所有者的电子邮件:谁是你

别忘了编辑:{otherUsersEmail}。。。发送到其他用户的电子邮件地址

参考: