【问题标题】:How can I view the path from the 'google drive api' file list如何从“google drive api”文件列表中查看路径
【发布时间】:2019-05-21 02:12:43
【问题描述】:

我已经关注https://developers.google.com/drive/api/v2/reference/files/list,成功了!我明白了。但不是我的“驱动器”。我收到一条警告“您需要在此处获得访问权限。那么我该如何解决上述错误呢?

我创建了一个新项目、服务帐号密钥和 OAuth 2.0 客户端 ID。但很糟糕,它没有正确链接到我的“google drive”帐户

这是我的全部代码

<?php
    session_start ();

    require_once 'google-api-php-client/src/Google_Client.php';
    require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
    require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
    require_once 'vendor/autoload.php';

    $DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
    $SERVICE_ACCOUNT_EMAIL = 'xxx@phim-240702.iam.gserviceaccount.com';
    $SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'phim-240702-a98343eb742a.p12';

    function buildService(){
      global $DRIVE_SCOPE, $SERVICE_ACCOUNT_EMAIL, $SERVICE_ACCOUNT_PKCS12_FILE_PATH;
      $key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
      $auth = new Google_AssertionCredentials(
        $SERVICE_ACCOUNT_EMAIL,
        array($DRIVE_SCOPE),
        $key);

    $client = new Google_Client();

    $client->setApplicationName("googletest5");
    $client->setDeveloperKey("b2016fa55e916faf35337ccb1db830ecdb590cc3");
    $client->setUseObjects(true);
    $client->setAssertionCredentials($auth);
    return new Google_DriveService($client);
    }

    /**
     * Insert new file.
     *
     * @param Google_Service_Drive $service Drive API service instance.
     * @param string $title Title of the file to insert, including the extension.
     * @param string $description Description of the file to insert.
     * @param string $parentId Parent folder's ID.
     * @param string $mimeType MIME type of the file to insert.
     * @param string $filename Filename of the file to insert.
     * @return Google_Service_Drive_DriveFile The file that was inserted. NULL is
     *     returned if an API error occurred.
     */
    function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
      $file = new Google_DriveFile();
      $file->setTitle($title);
      $file->setDescription($description);
      $file->setMimeType($mimeType);

      // Set the parent folder.
      if ($parentId != null) {
        $parent = new Google_Service_Drive_ParentReference();
        $parent->setId($parentId);
        $file->setParents(array($parent));
      }

      try {
        $data = file_get_contents($filename);

        $createdFile = $service->files->insert($file, array(
          'data' => $data,
          'mimeType' => '$mimeType',
        ));

        return $createdFile;
      } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
      }
    }

    /**
     * Retrieve a list of File resources.
     *
     * @param Google_Service_Drive $service Drive API service instance.
     * @return Array List of Google_Service_Drive_DriveFile resources.
     */
    function retrieveAllFiles($service) {
      $result = array();
      $pageToken = NULL;

      do {
        try {
      $parameters = array(
        'maxResults' => '100'
        );
          if ($pageToken) {
            $parameters['pageToken'] = $pageToken;
          }
          $files = $service->files->listFiles($parameters);

          $result = array_merge($result, $files->getItems());
          $pageToken = $files->getNextPageToken();
        } catch (Exception $e) {
          print "An error occurred: " . $e->getMessage();
          $pageToken = NULL;
        }
      } while ($pageToken);
      return $result;
    try {

      $root_id = null;
      $service = buildService();
       print_r(retrieveAllFiles($service));
      }catch (Exception $e) {
         print "An error occurred: " . $e->getMessage();
      }
    ?>

【问题讨论】:

  • 我刚刚开始了解“google drive api”。期待收到大家的建议

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


【解决方案1】:

您需要记住的是,服务帐户不是您。服务帐户是一个虚拟用户。它有自己的谷歌驱动器帐户。当您将文件上传到服务帐户驱动器帐户时,它们只能由该帐户访问,因为它是它的所有者。

共享驱动器

如果您希望它能够访问您的个人云端硬盘帐户中的文件,您需要通过与服务帐户电子邮件地址共享文件来授予其访问权限。这就是预先批准服务帐户的方式。

获取路径

至于获取文件的路径,没有简单的方法可以做到这一点,您需要通过获取文件,然后获取其父级,然后获取更多父级,直到您到达顶部和以这种方式构建它。

【讨论】:

  • 您的评论帮助我理解了问题所在。非常感谢。
  • 我修复了上述错误。但我想利用“downloadUrl”链接下载文件。但我收到警告“此页面无法正常工作如果问题仍然存在,请联系网站所有者。HTTP ERROR 401”。不能使用是浪费。你能解释一下帮我解决这个问题吗?非常感谢。
  • 您可能希望将其作为一个新问题打开并接受这个问题,因为这与第一个问题无关。但是,downloadurl 仅适用于实际有权访问该文件的用户,您需要授予用户对该文件的权限。您可以尝试将文件设置为公开,但每个人都应该有权下载它。跨度>
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多