gpt4 book ai didi

PHP Google Contact API 生成 HTTP/1.0 401 需要授权

转载 作者:搜寻专家 更新时间:2023-10-31 22:11:27 24 4
gpt4 key购买 nike

我正在使用带有 OAuth 2.0 的 Google Contact API V3 从 Google 帐户检索联系人,我使用 jQuery 从子窗口获取 OAuth token ,然后将该 token 发布到另一个应该获取用户联系人的文件,但是当我传递 token 以从 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>";
}
}
?>

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

最佳答案

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

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

extension=php_openssl.dll    
allow_url_fopen = On

关于PHP Google Contact API 生成 HTTP/1.0 401 需要授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12410626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com