如何通过PHP更新Google Contact的照片?

时间:2011-11-19 19:50:49

标签: php curl google-api google-contacts

我正在用PHP编写一个网站,允许用户在一个长列表中查看他所有的Google联系人姓名和个人资料照片。然后,用户可以拖放新照片以替换现有的个人资料照片。

但是,我在尝试更新照片时遇到401(未经授权的访问)错误。

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

docs没有多大帮助。另请参阅thesethese

我的代码类似于:

#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: Bearer'行替换为Authorization: GoogleLogin auth=ya29.AHES6ZR_ZS7_u-8TbMOMaeaGW9q4F7tkuPxW6gFTulyBX

等变体

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

我无法相信它有效,但将image/*更改为image/jpg修复了401错误问题。

This post让我失望。)

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