将文件上传到Google云端硬盘时出现问题,但我无法弄清楚如何操作。我可以成功检索我的文件,但无法上传。
这是我的错误:
{ "error":
{ "errors":
[ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ],
"code": 401, "message": "Invalid Credentials"
}
}
但是,我的应用程序已在驱动器SDK中注册,并且我正在使用驱动器SDK客户端ID。并且令牌有效,因为它在从驱动器检索文件时起作用。
这是代码:
function create_file($token){
$file = file_get_contents("mynewsuper.txt");
$length = strlen($file);
$url="https://www.googleapis.com/upload/drive/v2/files";
$header = array(
"Host: www.googleapis.com",
"Authorization:Oauth $token");
$ch = curl_init();
$post = array(
"file"=>"@mynewsuper.txt",
);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,false);
curl_setopt ($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
$data = curl_exec($ch);
echo $data;
}