簡體   English   中英

我無法使用php在我的Google雲端硬盤中看到通過api創建的文件和文件夾

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

我正在嘗試使用谷歌驅動器api創建文件夾。 我能夠在最后得到文件ID。 但我無法在谷歌驅動器中的文件夾。 看來有些許可問題?

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

正在創建的文件屬於API帳戶電子郵件(類似於api-service-account@yourproject.iam.gserviceaccount.com 。如果您轉到驅動器URL: https://docs.google.com/document/d/{ID}api-service-account@yourproject.iam.gserviceaccount.com https://docs.google.com/document/d/{ID} ,您將收到“權限被拒絕,請求訪問”頁面。

服務帳戶電子郵件與您的用戶電子郵件無關。

要與用戶一起創建文件,您需要創建OAauth令牌授權。

  1. 按照此頁面上的“步驟1:打開Drive API”步驟創建OAuth憑據

  2. 修改您的腳本,按照此頁面上的示例,您可以:


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

該文件應位於Drive主目錄中。

順便說一句,我建議你嘗試新版本google / apiclient:2.0.2 (仍在測試版,但工作正常)。

Mayrop答案部分正確。

正在創建的文件屬於API帳戶電子郵件(類似於api-service-account@yourproject.iam.gserviceaccount.com。

您需要向該帳戶的所有者授予權限

    $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();
  }

您將在與我共享的文件夾中看到該文件夾​​。 您也可以手動添加驅動器。

您還可以使用電子郵件禁用發送通知

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

希望這對你有用。

是的,似乎是權限問題。 嘗試刪除

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM