【问题标题】:PHP gdrive upload filePHP gdrive 上传文件
【发布时间】:2017-10-24 12:51:01
【问题描述】:

没有错误。为什么没有发送文件?

error_reporting(E_ALL);
    ini_set('display_errors',1);
         try{       
    require_once './vendor/autoload.php';

    $client = new Google_Client();

    $oauth_credentials = './vendor/test.json';


    $client = new Google_Client();
    $client->setAuthConfig($oauth_credentials);
    $client->setScopes('https://www.googleapis.com/auth/drive');


    $driveService = new Google_Service_Drive($client);

    $fileMetadata = new Google_Service_Drive_DriveFile(array(
        'name' => 'photo.png'));


    $content = file_get_contents('./vendor/plik.png');
    $file = $driveService->files->create($fileMetadata, array(
        'data' => $content,
        'mimeType' => 'image/png',
        'uploadType' => 'multipart',
        'fields' => 'id'));
    printf("File ID: %s\n", $file->id);

    }catch(Exception  $e){
    echo $e->getMessage();
    }

【问题讨论】:

  • 欢迎来到 Stack Overflow。我们不会去其他地方弄清楚你在问什么。所有相关代码都必须在这里,在问题本身中。请参阅 How to Askminimal reproducible example,然后返回并 edit 您的帖子以包含演示问题所需的最少代码量。您还需要包含用于编写代码的语言的标签,因为这显然是相关的。 (是的,我在标题中看到了 PHP,但它也应该在标签中,以供将来的读者在此处搜索时使用。)
  • 请不要在 Stack Overflow 上发布像 paste.ofcode.org 这样的临时内容。

标签: php google-api google-drive-api google-api-php-client


【解决方案1】:

您似乎正在使用服务帐户。服务帐户不是您。使用服务帐户上传的文件将上传到服务帐户 google drive 帐户。

在 google drive 中使用服务帐户的提示:

  1. 不,服务帐户文件没有 Web 视图。
  2. 执行 file.list 以查看该文件是否确实已上传到您的服务帐户。
  3. 在您的个人 google drive 帐户上共享一个目录,并将服务帐户上传到该目录。请记住授予自己对该文件的权限。

【讨论】:

  • 好的,文件显示有效.. 但是......如何共享这个文件夹?在这里看到drive.google.com/drive
猜你喜欢
  • 2018-07-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 2011-01-26
  • 2017-01-18
  • 1970-01-01
相关资源
最近更新 更多