3

I am using Google Contact API V3 with OAuth 2.0 to retrieve the contacts from Google Account, I am getting a OAuth Token from a child window using jQuery, and after post that token to another file which should get the User's Contacts, but when I pass the token to get the contacts from Google, but it gives an error "Warning: file_get_contents(https://www.google.com/m8/feeds/contacts/default/full&oauth_token=[token]) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 401 Authorization required in ...\getContacts.php on line 7"

Here is my code for reference:

In my index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <script src="js/jquery-1.8.1.min.js" type="text/javascript"></script>
   <script type="text/javascript">
      var windowSizeArray = [ "width=200,height=200",
                        "width=300,height=400,scrollbars=yes" ];

      $(document).ready(function(){
         $('#newWindow').click(function (event){
            var url = $(this).attr("href");
            var windowName = "popUp";//$(this).attr("name");
            var windowSize = windowSizeArray[  $(this).attr("rel")  ];
            window.open(url, windowName, windowSize);
            event.preventDefault();
         });
      });

      function getContacts(accessToken){
          $.post("getContacts.php?token="+accessToken,function(data){
            $("#contacts").html(data);
          });
      }
   </script>
</head>

<body>

<a href="https://accounts.google.com/o/oauth2/auth?client_id=xxxxxxxxxxxxxxxx&redirect_uri=xxxxxxxxx&scope=https://www.google.com/m8/feeds/&response_type=code" id="newWindow">Click Here</a>! to import your contact.

<div id="contacts"></div>

in my childWindow.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="js/jquery-1.8.1.min.js"></script>
</head>

<body>

<?php
    $authcode= $_GET["code"];    
    $clientid='My Client ID';         
    $clientsecret='My Client Secret';         
    $redirecturi='http://localhost/googleContacts/validate.php';  

    $fields=array(
                    'code'=>  urlencode($authcode),
                    'client_id'=>  urlencode($clientid),
                    'client_secret'=>  urlencode($clientsecret),
                    'redirect_uri'=>  urlencode($redirecturi),
                    'grant_type'=>  urlencode('authorization_code')
                );  

    //url-ify the data for the POST 
    $fields_string='';
    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
    $fields_string=rtrim($fields_string,'&');

    //open connection
    $ch = curl_init();

    //set the url, number of POST vars, POST data
    curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
    curl_setopt($ch,CURLOPT_POST,5);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

    // Set so curl_exec returns the result instead of outputting it.
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //to trust any ssl certificates
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    //execute post
    $result = curl_exec($ch);

    //close connection
    curl_close($ch);

    //extracting access_token from response string
    $response=  json_decode($result);
    $accesstoken= $response->access_token;

    //echo "<span>".$accesstoken."</span><hr>";
?>

<script type="text/javascript">
    $(document).ready(function() {
        window.opener.getContacts("<?php echo $accesstoken; ?>");
        window.close();
    });
</script>

And finally in my getContacts.php:

<?php

$accesstoken = $_REQUEST['token'];

//passing accesstoken to obtain contact details  

$xmlresponse=  file_get_contents('https://www.google.com/m8/feeds/contacts/default/full&oauth_token='.$accesstoken);  

//reading xml using SimpleXML  

$xml=  new SimpleXMLElement($xmlresponse);  

foreach($xml->entry as $content){

    $nameEmail = "";

    if(!empty($content->title)){
        $nameEmail .= "<span style=\"text-decoration:underline;\">".$content->title."</span>: ";    
    }

    $gd = $content->children('http://schemas.google.com/g/2005');

    if($gd){
        $nameEmail .= $gd->attributes()->address."<hr>";
        echo $nameEmail;
    }else{
        echo $nameEmail."<hr>";
    }
} 
?>  

Please tell me where is the mistake. Thanks in advance

Hassam
  • 253
  • 6
  • 16

1 Answers1

0

To allow https for file_get_contents() you should have enabled the php extension php_openssl.dll.

Make sure that in your php.ini you have this lines:

extension=php_openssl.dll    
allow_url_fopen = On
Emil Condrea
  • 9,705
  • 7
  • 33
  • 52