0
votes

I want to upload a file to google drive for that i have used following code and google drive sdk but i am getting following errors,Error calling POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart: (401) Login Required. I do not have much knowledge about integration of google drive sdk with php. Please help to solve it.

<?php

    include_once 'google-api-php-client-master/src/Google/Client.php';
    include_once 'google-api-php-client-master/src/Google/Service/Drive.php';
    include_once 'google-api-php-client-master/src/Google/Service/Oauth2.php';

    $client = new Google_Client();

    $client->setScopes(array('https://www.googleapis.com/auth/drive.file'));
    $client->setClientId('------');
    $client->setClientSecret('---------------');
    $client->setRedirectUri('https://www.example.com/oauth2callback');


    $service = new Google_Service_Drive($client);

    $data = file_get_contents("a.jpg");

    // create and upload a new Google Drive file, including the data
    try
    {
    //Insert a file
    $file = new Google_Service_Drive_DriveFile($client);

    $file->setTitle(uniqid().'.jpg');
    $file->setMimeType('image/jpeg');

    $createdFile = $service->files->insert($file, array('data' => $data,'mimeType' => 'image/jpeg','uploadType' => 'media',));
    }
    catch (Exception $e)
    {
        print $e->getMessage();
    }

    ?>
1

1 Answers

0
votes

You have to set up a new project under Google Developers Console. You can read more about the process, as well as a sample project, here: https://developers.google.com/drive/web/examples/php