3

我正在使用带有 OAuth 2.0 的 Google Contact API V3 从 Google 帐户检索联系人,我使用 jQuery 从子窗口获取 OAuth 令牌,然后将该令牌发布到另一个应该获取用户联系人的文件,但是当我通过从 Google 获取联系人的令牌,但它给出了错误“ 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

这是我的参考代码:

在我的 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>

在我的 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>

最后在我的 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>";
    }
} 
?>  

请告诉我错误在哪里。提前致谢

4

1 回答 1

0

要允许 https 为file_get_contents()您应该启用 php 扩展php_openssl.dll

确保在您的 php.ini 中有以下行:

extension=php_openssl.dll    
allow_url_fopen = On
于 2014-02-28T06:30:12.320 回答