下面是我在控制器中代码
构造函数代码
private $drive;
public function __construct(\Google_Client $client)
{
$this->middleware(function ($request, $next) use ($client) {
$accessToken = [
'access_token' => auth()->user()->token,
'created' => auth()->user()->created_at->timestamp,
'expires_in' => auth()->user()->expires_in,
'refresh_token' => auth()->user()->refresh_token
];
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
}
Auth::user()->update([
'token' => $client->getAccessToken()['access_token'],
'expires_in' => $client->getAccessToken()['expires_in'],
'created_at' => $client->getAccessToken()['created'],
]);
}
$client->refreshToken(auth()->user()->refresh_token);
$this->drive = new \Google_Service_Drive($client);
return $next($request);
});
}
上传文件到驱动器的方法我想问题就出在这个方法上
function createFile($file, $parent_id = null){
$fileName = FileStorage::find($file)->first();
$name = pathinfo(asset('uploadedfiles/' . $fileName->filenames));
$meta = new \Google_Service_Drive_DriveFile([
'name' => $name['basename'],
'parent' => '1GJ3KC-vsBrLAtlwUYgOvm7AjrtIXb4t-',// Parent Folder ID
]);
$content = File::get('uploadedfiles/' . $fileName->UniqueFileName);
$mime = File::mimeType('uploadedfiles/' . $fileName->UniqueFileName);
$file = $this->drive->files->create($meta, [
'data' => $content,
'mimeType' => $mime,
'uploadType' => 'multipart',
'fields' => 'id',
]);
}
我的代码有什么问题?2任何帮助都将不胜感激。
1条答案
按热度按时间5vf7fwbs1#
它将转到根目录的原因是您没有设置父文件夹。您使用了参数
parent
,而实际上您应该使用的参数是parents
,并且值应该是一个数组。parent只是被忽略,因为它不是一个有效的参数变更
至