Jump to content

gmail data importing trouble in oauth


pmtramu

Recommended Posts

    <?php
    
   //here code for gdata  retriving access
 
    $client_id = '903902416168.apps.googleusercontent.com';
    $client_secret = '1CQjbjx3tGbovyesww10D1U3';
    $redirect_uri = 'http://localhost/oauth/oauth.php';
    $max_results = 50;
    $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, 0);    //To stop cURL from verifying the peer's certificate.
     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);
    
    ?>
 //here $xml get data from gmail
   
    
    
    
 <?php
    $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
    
/*if print_r($xml);
am getting array values
*/  
    foreach ($xml as $gname) {
//here am getting error as Fatal error: Call to a member function attributes() on a non-object
       echo $gname->entry->title  . " " . $gname->xpath('//gd:email')->attributes()->address;
    }
      
    ?>
 

Link to comment
https://forums.phpfreaks.com/topic/282558-gmail-data-importing-trouble-in-oauth/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.