Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't see the files and folders created via api in my Google Drive using php

I am trying to create folder using google drive api. I am able to get file id at the end. but i am not able to my folder in google drive. it seems some permission issue?

$scopes = array(
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.appdata','https://www.googleapis.com/auth/drive.apps.readonly',
    'https://www.googleapis.com/auth/drive.metadata','https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/drive.readonly','https://www.googleapis.com/auth/drive.photos.readonly'
);
$creds = new Google_Auth_AssertionCredentials(
    $serviceAccountName,
    $scopes,
    file_get_contents($keyFile)
);
$client = new Google_Client();
$client->setApplicationName($appName);
$client->setClientId($clientId);
$client->setAssertionCredentials($creds);
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
  'name' => 'Invoices',
  'permissions' => array('type'=>'anyone','role'=>'reader','allowFileDiscovery'=>1),

  'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array());

printf("File ID: %s\n", $file->id);
like image 604
Ankita Kashyap Avatar asked Jul 27 '16 13:07

Ankita Kashyap


People also ask

Why is Google Drive not showing folders?

Check Backup and Sync Preferences To do that, right-click the Backup and Sync icon on the system tray and select Settings > Preferences. Then, switch to the Google Drive tab to make sure you've selected the files and folders you want to sync correctly.

Why I Cannot see files in Google Drive?

If a file won't open, a few things could be wrong: The file owner didn't give you permission to view the file. You're signed in to a different Google Account. Your access could be denied because someone removed your permission to view the file.

How do I open Google API files?

Go to the Google API Console. Select a project. In the sidebar on the left, expand APIs & auth and select APIs. In the displayed list of available APIs, click the Drive API link and click Enable API.


2 Answers

The files that are being created belong to the API account email (something like [email protected]. If you go to the drive URL: https://docs.google.com/document/d/{ID}, you will get a "Permission Denied, request access" page.

The service account email is not related to your user email.

In order to create files with your user, you need to create an OAauth token authorization.

  1. Create the OAuth credentials following the steps "Step 1: Turn on the Drive API" on this page

  2. Modify your script, by following the example on this page, you can have:


use Google_Client;
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;

...

$file = 'root/to/your/credentials.json';

$client = new Google_Client();
$client->setScopes([
    'https://www.googleapis.com/auth/drive',
    'https://www.googleapis.com/auth/drive.appdata',
    'https://www.googleapis.com/auth/drive.apps.readonly',
    'https://www.googleapis.com/auth/drive.metadata',
    'https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/drive.readonly',
    'https://www.googleapis.com/auth/drive.photos.readonly'
]);

$client->setAuthConfig($file);
$authUrl = $client->createAuthUrl();

printf("Open the following link in your browser:\n%s\n", $authUrl);

// You will need to open this url
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));

$accessToken = $client->authenticate($authCode);

file_put_contents($credentialsPath, $accessToken);
printf("Credentials saved to %s\n", $credentialsPath);

$client->setAccessToken(json_decode($accessToken, true));

$service = new Google_Service_Drive($client);

$metadata = new Google_Service_Drive_DriveFile([
    'name' => 'testing drive',
    'mimeType' => "application/vnd.google-apps.document"
]);
$file = $service->files->create($metadata, []);
print_r($file);

The file should be in your Drive home directory.

By the way, I suggest you try the new version google/apiclient:2.0.2 (which is still on Beta, but works okay).

like image 105
mayrop Avatar answered Sep 28 '22 11:09

mayrop


Mayrop answer is partially right.

The files that are being created belong to the API account email (something like [email protected].

You need to give permission to owner of that account like

    $newPermission = new Google_Service_Drive_Permission();
$value="email id";
$type="user";
$role="writer";
  $newPermission->setValue($value);
  $newPermission->setType($type);
  $newPermission->setRole($role);
 try {
   $res= $service->permissions->insert($fileId, $newPermission);
    print_r($res); 
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }

You will see that folder in Shared with me. You can also add in your drive manually.

You can also disable sending notification via email using

$service->permissions->insert($fileId, $newPermission,array('sendNotificationEmails'=>false));

Hope this will work for you.

like image 37
Hitu Bansal Avatar answered Sep 28 '22 11:09

Hitu Bansal