I
I
IvanN7772019-05-28 17:02:25
PHP
IvanN777, 2019-05-28 17:02:25

How to make a file public in google drive api?

<?php
require __DIR__ . '/vendor/autoload.php';


class UploadPresentation
{
  public function execute($filePath, $title)
  {
    $client = $this->getClient();
    $driveService = new \Google_Service_Drive($client);
    //$fileId = $this->uploadFile($driveService, $filePath, $title);
    $fileId = '1ZhBOGeLYLZS5gFsMNf-OoiDSrxIF23ilFXEKKRdpAWw';
    $file = $driveService->files->get($fileId, array("fields"=>"webViewLink"));
    print($file->webViewLink);
  }

  private function getClient()
  {
    $client = new \Google_Client();
    $client->setApplicationName('My PHP App');
    $client->setScopes([\Google_Service_Drive::DRIVE]);
    $client->setAccessType('offline');
    $jsonAuth = file_get_contents('credentials.json');
    $client->setAuthConfig(json_decode($jsonAuth, true));
    return $client;
  }

  private function uploadFile(Google_Service_Drive $driveService, $filePath, $title)
  {
    $date = new \DateTime();
    $timestamp = $date->getTimestamp();
    $timestamp = '1';
    $file = new \Google_Service_Drive_DriveFile([
      'name'     => $title.$timestamp,
      'mimeType' => 'application/vnd.google-apps.presentation'
    ]);
    $params = [
      'uploadType' => 'multipart',
      'data' => file_get_contents($filePath),
      'mimeType' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
    ];
    $upload = $driveService->files->create($file, $params);
    $fileId = $upload->id;
    return $fileId;
  }
}
$upload = new \UploadPresentation();
$upload->execute('/home/ivan/1_projects/drive/test.pptx', 'test');

The trouble is that the file is private, and I would like to allow everyone to view it.
Nobody faced?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2019-05-28
@dimonchik2013

not enough code
https://developers.google.com/drive/api/v2/manage-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question