【问题标题】:How to see files uploaded to google drive via PHP?如何查看通过 PHP 上传到谷歌驱动器的文件?
【发布时间】:2019-08-08 21:56:24
【问题描述】:

我一直在尝试通过 service-account 使用 php 将文件上传到我的 Google Drive 帐户。我猜文件已成功上传,因为我可以打印文件 ID、文件创建时间和所有其他信息。

这是我的问题:当我登录我的 google drive 帐户时,我看不到任何上传的文件。文件隐藏在哪里或者我该怎么办?

代码如下:

require __DIR__ . '/vendor/autoload.php'; //api
$serviceAccount     = "xxx@xxxxxx.iam.gserviceaccount.com";
$key_file       = "mykey-goes-here.p12";
$auth = new Google_Auth_AssertionCredentials(
                    $serviceAccount,
                    array('https://www.googleapis.com/auth/drive.file'),
                    file_get_contents($key_file)
                    );
$client = new Google_Client();
$client->setAssertionCredentials( $auth );

$service = new Google_Service_Drive($client);

 $file = new Google_Service_Drive_DriveFile();
  $file->setTitle("my file name");
 $file->setDescription('testing desc');
 $file->setMimeType('text/plain');
                // Provide file name here and path in below 

  $result = $service->files->insert($file, array(
  'data' => file_get_contents("test.txt"),
//'mimeType' => 'text/plain',
  'mimeType' => 'application/octet-stream',
  'uploadType' => 'media',
 //'uploadType' => 'multipart'
));

 // Uncomment the following line to print the File ID
   printf("File ID: %s\n", $result->id);


echo "<br><br>";
echo '<pre>'; print_r($result); echo'</pre>';

【问题讨论】:

  • 如果手动上传文件,可以用PHP代码访问吗?

标签: php google-drive-api


【解决方案1】:

解决方案:

我发现服务帐户上传的文件无法通过 UI 访问。

要解决此问题,需要委托/授权应用程序使用所述服务帐户。 link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多