【问题标题】:How do I get a file list for a Google Drive public hosted folder?如何获取 Google Drive 公共托管文件夹的文件列表?
【发布时间】:2013-08-08 00:45:33
【问题描述】:

有没有办法不使用授权获取公开托管的 Goolge Drive 文件夹的文件列表?

示例文件夹:https://googledrive.com/host/0B1Nmfb7VDM6jNnhjWk9CdU9ueHc/

我想使用该文件夹来托管我将在其他网站的图库中显示的图像。

【问题讨论】:

标签: google-drive-api


【解决方案1】:

好的,有一种方法,它非常简单,只需要一些设置。我们需要获取一个可以返回 JSONP 的 Google Drive URL,然后针对它运行请求。

设置

生成公共 URL。去: https://developers.google.com/apis-explorer/?hl=en_GB#p/drive/v3/drive.files.list

在“q”字段中输入以下内容:

'{your_public_folder_id}' in parents

单击按钮下方的“不使用 OAuth 执行”链接。

您现在应该会看到下面列出的所有文件。它还会向您显示 GET 请求这是您需要的网址。

复制请求 URL。类似于:https://www.googleapis.com/drive/v3/files?q='{your_public_folder_id}'+in+parents&key={YOUR_API_KEY}

现在,您需要生成 API 密钥。 转到 Google 控制台:https://console.developers.google.com/ 在左侧菜单中选择“凭据”。 单击“创建凭据”并选择 API 密钥。 点击“浏览器键” 复制生成密钥。

执行

您现在可以执行以下操作:

var api_key = '{YOUR_API_KEY}';
var folderId = '{your_public_folder_id}';
var url = "https://www.googleapis.com/drive/v3/files?q='" + folderId + "'+in+parents&key=" + api_key;
var promise = $.getJSON( url, function( data, status){
    // on success
});
promise.done(function( data ){
    // do something with the data
}).fail(function(){

});

【讨论】:

  • 太棒了!!超酷!!我猜是在没有身份验证的情况下获取文件夹子项的唯一方法/解决方法!荣誉@iDevGeek :)
  • 这太棒了!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!顺便说一句,对于其他任何人:一旦您拥有 API 密钥,您就可以通过以下链接获得指向 Google 驱动器中任何大小文件的直接下载链接:googleapis.com/drive/v3/files/### fileId ###?alt=media&key=### API 密钥### 查看更多:stackoverflow.com/questions/54588701/…
  • 可以在没有您的密钥的情况下共享完整链接吗?或使用 api exploerer 的示例
【解决方案2】:

有一种列出公用文件夹的简单方法 - 只需使用 YQL 即可获取 XML 或 JSON 结果。

【讨论】:

    【解决方案3】:

    没有办法在未经身份验证的情况下进行。但是,您可以以任何用户身份进行身份验证并使用drive.children.list() 获取列表

    【讨论】:

    • 如何在服务器上创建服务实例?
    • 事实并非如此。也许是在发布此内容时,但您无需进行身份验证即可获取公用文件夹的内容。您仍然需要一个 API 密钥。请在下面查看我的答案。
    【解决方案4】:

    另一种解决方案——不使用任何 API 密钥——是使用 google app-script 为目录生成一个文本文件,然后每天左右将该脚本放在触发器上,以便在目录更改时刷新它,然后只需制作由 google 应用程序脚本修改的 google doc 文件,最后加上公共 URL 加上 /export?format=txt,或者用于常规文件,例如 https://drive.google.com/uc?export=download&id=INSERT_ID_HERE,然后可以在网络上公开访问未经授权

    【讨论】:

      【解决方案5】:

      对于那些对此请求仍有疑问的人,只需使用此模板:https://www.googleapis.com/drive/v3/files?q=%27{YOUR FOLDER ID}%27+in+parents&key={YOUR API KEY},不带括号。 这对我很有帮助,因为这些“'”字符的问题是拖累

      【讨论】:

        【解决方案6】:

        在 PHP 中你可以这样做

        • 在您的 Google Cloud Console 中创建一个项目
        • 通过进入导航菜单 > IAM 和管理 > 服务帐户获取服务帐户 JSON 文件
        • 运行以下代码以列出您请求的文件夹 ID 中的所有文件
            <?php
        
            require __DIR__ . '/vendor/autoload.php';
        
            if (php_sapi_name() != 'cli') {
                throw new Exception('This application must be run on the command line.');
            }
        
            $client = new Google_Client();
            // set the location manually
            $client->setAuthConfig('credentials.json');
            $client->setApplicationName("My Application");
            $client->setScopes(Google_Service_Drive::DRIVE_METADATA_READONLY);
            $service = new Google_Service_Drive($client);
        
            // Print the names and IDs for up to 100 files.
            $optParams = array(
                'pageSize' => 100,
                'fields' => 'nextPageToken, files(id, name)',
                'q' => "'YOUR_FOLDER_ID' in parents",
            );
            $results = $service->files->listFiles($optParams);
        
            if (count($results->getFiles()) == 0) {
                print "No files found.\n";
            } else {
                print "Files:\n";
                foreach ($results->getFiles() as $file) {
                    printf("%s (%s)\n", $file->getName(), $file->getId());
                }
            }
        

        【讨论】:

          【解决方案7】:

          您的扩展 google 驱动器 url 是 this ,因此请使用以下扩展 url 来获取所有以“.html”扩展名结尾的文件。此示例使用 jquery 使用 ajax 调用。

          根据需要调整脚本。

          <script type="text/javascript">
          var dir = "https://d95b2b8c54c1827d7a316594f5436a81b5bb2b76.googledrive.com/host/0B1Nmfb7VDM6jNnhjWk9CdU9ueHc/";
          var fileextension = ".html";
          $.ajax({
          //This will retrieve the contents of the folder if the folder is configured as 'browsable'
          url: dir,
          success: function (data) {
              //Lsit all png file names in the page
              $(data).find("a:contains(" + fileextension + ")").each(function () {
                  var filename = this.href.replace(window.location.host, "").replace("http://", "").replace("https://", "");
                  console.log(filename);
                  filename = filename.split("/").pop();
                  $("body").append($("<a href=" + dir + filename + ">" + filename + "</a>"));
              });
              });
          }
          });
          </script>
          

          这会将所有精细名称(.html)记录到控制台并将每个文件名的超链接添加到正文末尾。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-08-05
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多