Cakephp: Загрузить файл с Google Drive API - PullRequest
0 голосов
/ 08 апреля 2019

Я пытаюсь загрузить файл с локального на диск Google в моем приложении Cake PHP. Эта функция как автономный скрипт выполняется без проблем, но когда я пытаюсь интегрировать его в приложение 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());
    }
}
...