将文件上传到 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;
}