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

5

14

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);
Revengeful answered 27/7, 2016 at 13:52 Comment(0)
F
3

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.

Frazzle answered 8/8, 2016 at 5:45 Comment(2)
while using this code I am getting Call to undefined method Google_Service_Drive_Permission::setValue() ? why is this happening ?Dysfunction
@Dysfunction Use: $newPermission->setEmailAddress($value); I am also writing the full answer that worked for me here for more details.Guest
A
4

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).

Allergic answered 2/8, 2016 at 6:52 Comment(0)
F
3

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.

Frazzle answered 8/8, 2016 at 5:45 Comment(2)
while using this code I am getting Call to undefined method Google_Service_Drive_Permission::setValue() ? why is this happening ?Dysfunction
@Dysfunction Use: $newPermission->setEmailAddress($value); I am also writing the full answer that worked for me here for more details.Guest
F
2

Yes, It seems permissions issue. Try removing

'permissions' => array('type'=>'anyone','role'=>'reader','allowFileDiscovery'=>1),

from

$fileMetadata = new Google_Service_Drive_DriveFile(array( 'name' => 'Invoices', 'permissions' => array('type'=>'anyone','role'=>'reader','allowFileDiscovery'=>1), 'mimeType' => 'application/vnd.google-apps.folder'));

Fjeld answered 1/8, 2016 at 3:51 Comment(0)
G
1

so I have worked over it, and finally got the full working solution for Google Drive V3 in Php. As follows:

You can go through this gist for full code and better understanding: https://gist.github.com/KartikWatts/22dc9eb20980b5b4e041ddcaf04caf9e

First of all adding to the answer from @Hitu Bansal: This code works fine:

$this->newPermission = new Google_Service_Drive_Permission();
$value=<YOUR EMAIL ADDRESS>;
$type="user";
$role="reader";
$this->newPermission->setEmailAddress($value);
$this->newPermission->setType($type);
$this->newPermission->setRole($role);
$this->newPermission->allowFileDiscovery;

try {
$res= $this->service->permissions->create($folder_id, $this->newPermission);
print_r($res);
catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
}

If, instead you want to share the link with anyone: You may use

    $type="anyone";
    $this->newPermission->setType($type);
    $this->newPermission->setRole($role);
    $this->newPermission->allowFileDiscovery;
    $this->newPermission->view;

NOTE:

  • In this case, setEmailAddress() shall be removed.
  • IMPORTANT: In case of 'anyone', the folder will not be visible in your personal drive for sure, but you can still visit and interact with the folder as per given roles by visiting the link making use of the folder_id that you know already. So, you can visit the link as: https://drive.google.com/drive/u/5/folders/**{folder_id}** It will be publically accessible now.

For more clear reference to the parameters along with a description, this is really helpful: https://developers.google.com/drive/api/v3/reference/permissions#methods

NOTE: In my personal opinion, a better way to work with Google Drive API is to create the folder first manually (if workflow allows it), then upload the files using the folder_id of that created folder. Remember, In case folder is created manually, permission shall be provided to the Service Account email-id before.

Guest answered 10/5, 2021 at 8:43 Comment(0)
D
1

While permission issue is the main cause of this problem. What I did to make the folders or files appear after I uploaded it with service account was to specify the parent folder. If you upload / create folder / files without parent folder ID, that object's owner will be the service account that you are using.
By specifying parent ID, it will use the inherited permissions.
Here's the code I use in php (google/apiclient)

$driveFile = new Google\Service\Drive\DriveFile();
$driveFile->name = 'New Folder';
$driveFile->mimeType = 'application/vnd.google-apps.folder';
$driveFile->parents = ['123456789qwertyuiop'];
$result = $service->files->create($driveFile);
Daloris answered 12/8, 2021 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.