如何通过 PHP 更新 Google 联系人的照片?
我正在用 PHP 编写一个网站,该网站将允许用户在一个长列表中查看他的所有 Google 联系人姓名和个人资料照片。然后,用户可以拖放新照片来替换现有的个人资料照片。
但是,我在尝试更新照片时收到 401(未经授权的访问)错误。
我知道 access_token 是正确的,因为它适用于 GET 请求,例如 https://www.google.com/m8/feeds/photos/media/default/6f2?access_token=ya29.AHES6ZR_ZS7_u-8TbMOMaeaGW9q4F7tkuPxW6gFTulyBX
我的代码看起来像这样:
#the stuff that i echo out is just for debugging purposes
$apiVersion='3.0';
$access_token = $this->session->userdata('googleauth_access_token');
$url = 'https://www.google.com/m8/feeds/photos/media/default/' . $contactID;
$fp = fopen($image_location, "r");
$filesize = filesize($image_location);
echo '<img src="https://www.google.com/m8/feeds/photos/media/default/'. $contactID. '?access_token=' . $access_token.'"/><br/>';
echo $image_location;
echo $filesize;
echo $url;
$requestHeaders=array('Content-type: image/*','If-Match: '.$eTag,'GData-Version: '.$apiVersion);
$requestHeaders[]='Authorization: Bearer '. $access_token;
print_r($requestHeaders);
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_PUT, true);
curl_setopt($c, CURLOPT_INFILESIZE, $filesize);
curl_setopt($c, CURLOPT_INFILE, $fp);
curl_setopt($c, CURLOPT_VERBOSE, true);
curl_setopt($c, CURLOPT_HTTPHEADER, $requestHeaders);
$result = curl_exec($c);
$curlGetInfo=curl_getinfo($c);
print_r($curlGetInfo);
curl_close($c);
fclose($fp);
echo $result;
我还尝试过各种变体,例如用 Authorization: GoogleLogin auth=ya29.AHES6ZR_ZS7_u-8TbMOMaeaGW9q4F7tkuPxW6gFTulyBX
替换 'Authorization: Bearer'
行
感谢您的帮助。
I am coding a site in PHP that will allow a user to view all his Google Contacts' names and profile photos in one long list. Then the user can drag-n-drop new photos to replace the existing profile photos.
However, I'm getting a 401 (unauthorized access) error when trying to update the photos.
I know the access_token is correct because it works in GET requests such as https://www.google.com/m8/feeds/photos/media/default/6f2?access_token=ya29.AHES6ZR_ZS7_u-8TbMOMaeaGW9q4F7tkuPxW6gFTulyBX
The docs have not helped much. See also these and these.
My code looks something like:
#the stuff that i echo out is just for debugging purposes
$apiVersion='3.0';
$access_token = $this->session->userdata('googleauth_access_token');
$url = 'https://www.google.com/m8/feeds/photos/media/default/' . $contactID;
$fp = fopen($image_location, "r");
$filesize = filesize($image_location);
echo '<img src="https://www.google.com/m8/feeds/photos/media/default/'. $contactID. '?access_token=' . $access_token.'"/><br/>';
echo $image_location;
echo $filesize;
echo $url;
$requestHeaders=array('Content-type: image/*','If-Match: '.$eTag,'GData-Version: '.$apiVersion);
$requestHeaders[]='Authorization: Bearer '. $access_token;
print_r($requestHeaders);
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_PUT, true);
curl_setopt($c, CURLOPT_INFILESIZE, $filesize);
curl_setopt($c, CURLOPT_INFILE, $fp);
curl_setopt($c, CURLOPT_VERBOSE, true);
curl_setopt($c, CURLOPT_HTTPHEADER, $requestHeaders);
$result = curl_exec($c);
$curlGetInfo=curl_getinfo($c);
print_r($curlGetInfo);
curl_close($c);
fclose($fp);
echo $result;
I have also tried variations such as replacing the 'Authorization: Bearer'
line with Authorization: GoogleLogin auth=ya29.AHES6ZR_ZS7_u-8TbMOMaeaGW9q4F7tkuPxW6gFTulyBX
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不敢相信它有效,但是将
image/*
更改为image/jpg
修复了 401 错误问题。(这篇文章给了我消息。)
另外仅供参考,我仍在使用
$requestHeaders[]='Authorization: Bearer '。 $access_token;
而不是$requestHeaders[]='Authorization: GoogleLogin auth='。 $access_token;
I can't believe it worked, but changing
image/*
toimage/jpg
fixed the 401 error problem.(This post tipped me off.)
Also FYI, I'm still using
$requestHeaders[]='Authorization: Bearer '. $access_token;
rather than$requestHeaders[]='Authorization: GoogleLogin auth='. $access_token;