Cakephp: Upload file with google drive API

General Tech Bugs & Fixes 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am trying to upload a file from local to google drive in my Cake PHP application. This function as a stand alone script runs without a problem but when I try to integrate it in the cakephp application it doesn't work. So it doesn't find any of these, they are null:

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

And the program exits with code 200.

This is the function in my cakephp application:

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.
                                                
                                                
0 views
0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.