2

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

但是当我在美国服务器的在线服务器上执行此操作时,它给了我“帐户已禁用”的响应,并且还收到了来自谷歌的同一用户的安全邮件。

我正在使用以下代码:

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;
    }
}

}

该邮件包含

“最近有人尝试使用应用程序登录您的 Google 帐户......位置:纽约 NY,纽约,美国......”

.

我认为此错误是由于位置更改而发生的。有人能帮帮我吗?提前致谢。

4

0 回答 0