Cakephp:使用Google Drive API上传文件

时间:2019-04-08 11:10:59

标签: php google-drive-api cakephp-3.0

我正在尝试在Cake PHP应用程序中将文件从本地上传到Google驱动器。此功能作为独立脚本运行时没有问题,但是当我尝试将其集成到cakephp应用程序中时,它将无法正常工作。 因此找不到任何这些,它们为空:

if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {

程序退出并显示代码200。

这是我的cakephp应用程序中的功能:

public function uploadFileToGoogleDrive($year, $month, $file_name, $full_path)
{
    $this->viewBuilder()->setLayout(false);
    $this->autoRender = false;

    $client = new Google_Client();

    // Get your credentials from the console
    $client->setClientId('CLIENT_ID');
    $client->setClientSecret('CLIENT-SECRET');
    $client->setRedirectUri('http://localhost/myapp');
    $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));

    session_start();

    if (isset($_GET['code']) || (isset($_SESSION['access_token']) && $_SESSION['access_token'])) {
        if (isset($_GET['code'])) {
            $client->authenticate($_GET['code']);
            $_SESSION['access_token'] = $client->getAccessToken();
        } else{
            $client->setAccessToken($_SESSION['access_token']);
        }

        $service = new Google_Service_Drive($client);

        //create the folder if doesn't exist
        //check if year folder exists:

        $parameters = ['q' => "name contains '".$year."' and mimeType contains 'application/vnd.google-apps.folder'"];

        $year_folders = $service->files->listFiles($parameters);

        if (count($year_folders) == 0){
            $fileFolderMetadata = new Google_Service_Drive_DriveFile();
            $fileFolderMetadata->setName($year);
            $fileFolderMetadata->setMimeType('application/vnd.google-apps.folder');

            $fileFolder = $service->files->create($fileFolderMetadata, array(
            'fields' => 'id'));
            //printf("Folder ID: %s\n", $fileFolder->id);

            //year folder id
            $year_folder_id = $fileFolder->id;

        }
        else{
            //year folder id
            $year_folder_id = $year_folders[0]->id;
        }

        //check if month folder exists

        $parameters = ['q' => "name contains '".$month."' and mimeType contains 'application/vnd.google-apps.folder'"];

        $month_folders = $service->files->listFiles($parameters);

        if (count($month_folders) == 0){
            $fileFolderMetadata = new Google_Service_Drive_DriveFile(
                ['parents' => [$year_folder_id]]
            );
            $fileFolderMetadata->setName($month);
            $fileFolderMetadata->setMimeType('application/vnd.google-apps.folder');

            $fileFolder = $service->files->create($fileFolderMetadata, array(
            'fields' => 'id'));
            //printf("Folder ID: %s\n", $fileFolder->id);

            //Insert a file
            $file = new Google_Service_Drive_DriveFile(
                ['parents' => [$fileFolder->id]]
            );
        }
        else{
            //Insert a file
            $file = new Google_Service_Drive_DriveFile(
                ['parents' => [$month_folders[0]->id]]
            );
        }

        $file->setName($file_name);
        $file->setDescription('Invoice');
        $file->setMimeType('application/octet-stream');

        $data = file_get_contents($full_path);

        $createdFile = $service->files->create($file, array(
              'data' => $data,
              'mimeType' => 'application/octet-stream',
              'uploadType' => 'multipart'
            ));
    }
    else{
        var_dump('failed: '.http_response_code());
    }
}

0 个答案:

没有答案