我正在用 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: Bearer'
用Authorization: GoogleLogin auth=ya29.AHES6ZR_ZS7_u-8TbMOMaeaGW9q4F7tkuPxW6gFTulyBX
谢谢你的帮助。