2012-09-13 112 views
3

我使用谷歌联系API V3与OAuth 2.0从Google帐户检索联系人,我从一个使用jQuery的子窗口获取OAuth令牌,并且在发布该令牌另一个文件,应该得到用户的联系人,但是当我通过令牌摆脱谷歌的联系,但它给出了一个错误“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 7PHP谷歌联系API生成HTTP/1.0 401需要授权

这里是我的参考代码:

在我的索引。 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>"; 
    } 
} 
?> 

请告诉我哪里是错误的。在此先感谢

回答

0

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

确保在您的php.ini你有这样的台词:

extension=php_openssl.dll  
allow_url_fopen = On 
+0

没有工作!同样的错误:( – thegeekajay

相关问题