使用谷歌API获取Gmail联系人,显示错误帐户被禁用

时间:2012-05-11 12:44:00

标签: php api curl google-api

我正在使用PHP。我想获取用户的所有gmail联系人,我正在使用通过CURL调用google API的PHP代码。但是,当我在localhost上执行此操作时,它表现良好并且给了我所有联系人。

但是当我在美国服务器的在线服务器上这样做时,它正在给我回复#34;帐户已禁用"并且还从谷歌收到同一用户的安全邮件。

我正在使用以下代码:

function getGmailContacts($user, $password) {
//========================================== step 1: login ===========================================================
$login_url = "https://www.google.com/accounts/ClientLogin";
$fields = array(
    'Email' => $user,
    'Passwd' => $password,
    'service' => 'cp', // <== contact list service code
    'source' => 'test-google-contact-grabber',
    'accountType' => 'GOOGLE',
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$login_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$fields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
$result = curl_exec($curl);

$returns = array();

foreach (explode("\n",$result) as $line)
{
    $line = trim($line);
    if (!$line) continue;
    list($k,$v) = explode("=",$line,2);

    $returns[$k] = $v;
}

curl_close($curl);

//echo "<pre>"; 
//print_r($returns);exit;

if(!isset($returns['Error'])) {

    //========================== step 2: grab the contact list ===========================================================
    $feed_url = "http://www.google.com/m8/feeds/contacts/$user/full?alt=json&max-results=250";

    $header = array(
        'Authorization: GoogleLogin auth=' . $returns['Auth'],
    );

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $feed_url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

    $result = curl_exec($curl);
    curl_close($curl);

    $data = json_decode($result, true);
    //echo "<pre>";
    //print_r($data);exit;
    $contacts = array();
    $i=0;
    foreach ($data['feed']['entry'] as $entry)
    {
        //echo $i." ";
        $entryElement = $entry;
        if(isset($entryElement['gd$email'])) {

            $gdEmailData = $entryElement['gd$email'][0];

            //$contact->title = $entryElement['title']['$t'];
            //$contact->email = $gdEmailData['address'];

            $contacts[$gdEmailData['address']] = $entryElement['title']['$t'];
        }
    }

    //var_dump($contacts);
    //print_r($contacts);
    return $contacts;
}
else {

    if($returns['Error']=='BadAuthentication') {

        //echo '<strong>User Name and Password is incorrect.</strong>';
        $errorArr = array("Error"=>"User Name and Password is incorrect.");
        //print_r($errorArr);
        return $errorArr;
    }
}

}

该邮件包含

  

&#34;最近有人尝试使用某个应用程序登录您的应用程序   Google帐户....地点:纽约纽约,纽约,美国.....&#34;

我发现这个错误是从位置变化发生的。 有人可以帮我吗?提前谢谢。

0 个答案:

没有答案