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

家住魔仙堡 提交于 2019-12-08 16:33:28

问题


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

回答1:


Mayrop answer is partially right.

The files that are being created belong to the API account email (something like api-service-account@yourproject.iam.gserviceaccount.com.

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.




回答2:


The files that are being created belong to the API account email (something like api-service-account@yourproject.iam.gserviceaccount.com. 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).




回答3:


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'));



来源:https://stackoverflow.com/questions/38614941/i-cant-see-the-files-and-folders-created-via-api-in-my-google-drive-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!