如何通过PHP更新Google Contact's照片


How to update Google Contact's photo via PHP?

我在PHP编码一个网站,将允许用户查看他所有的谷歌联系人的名字和个人资料照片在一个长列表。然后,用户可以拖放新照片来替换现有的个人资料照片。

然而,当我试图更新照片时,我得到了401(未经授权的访问)错误。

我知道access_token是正确的,因为它适用于GET请求,如https://www.google.com/m8/feeds/photos/media/default/6f2?access_token=ya29.AHES6ZR_ZS7_u-8TbMOMaeaGW9q4F7tkuPxW6gFTulyBX

文档没有帮助。参见these and these

我的代码看起来像这样:

#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'

谢谢你的帮助。

我不敢相信它工作了,但将image/*改为image/jpg修复了401错误问题。

(这篇文章给了我提示)

还供参考,我仍然使用$requestHeaders[]='Authorization: Bearer '. $access_token;而不是$requestHeaders[]='Authorization: GoogleLogin auth='. $access_token;