Fetch gmail contacts using google API, showing error account disabled

Discussion in 'Google API' started by chirayu.bansal, May 11, 2012.

  1. #1
    Hi,
    I am using PHP. I want to fetch all gmail contacts of a user, i am using a PHP code that is calling google API through CURL. But, when i am doing this on localhost, it is doing well and giving me all contacts.
    But when i am doing this on online server that server in US, it is giving me response "Account Disabled" and also receiving a security mail by same user from google.i am using below code :
    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;
    }
    }
    }That mail contains that
    "Someone recently tried to use an application to sign in to your Google Account....Location: New York NY, New York, United States....."
    .Please can any one help me? Thanks in advance.
     
    chirayu.bansal, May 11, 2012 IP
  2. azmoum

    azmoum Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    46
    #2
    can you export Google contact to an external file list ? like csv or txt ?
     
    azmoum, May 11, 2012 IP
  3. chirayu.bansal

    chirayu.bansal Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    no. i am just fetch contacts and save it in database
     
    chirayu.bansal, May 11, 2012 IP
  4. annaharryson

    annaharryson Peon

    Messages:
    207
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    no i dont know about it.
     
    annaharryson, May 19, 2012 IP
  5. Lynne A

    Lynne A Guest

    Messages:
    42
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You should ask this in google groups.

    You will get much better response there.
     
    Lynne A, May 22, 2012 IP
  6. Ida V

    Ida V Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I agree. This is more for a coding forum or google groups.
     
    Ida V, May 23, 2012 IP