doupu7651 2019-05-21 02:12
浏览 449

如何查看“google drive api”文件列表中的路径

I have followed https://developers.google.com/drive/api/v2/reference/files/list, successful! I got it all. But not my 'drive'. I received a warning 'You need to be granted access here. So what should I do to fix the above error?

I created a new project, Service account keys and OAuth 2.0 client IDs. but it's bad it doesn't properly link to my 'google drive' account

This is my whole code

<?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();
      }
    ?>
  • 写回答

1条回答 默认 最新

  • dongyong8098 2019-05-21 06:18
    关注

    What you need to remember is that a Service account is not you. A service account is a dummy user. It has its own google drive account. when you upload the files to the service accounts drive account they are only accessible by that account as it is the owner of it.

    Share a drive

    If you want it to be able to access files on your personal drive account you will need to grant it access to them by sharing them with the service accounts email address. This is how service accounts are pre approved.

    getting path

    As for getting the path of a file there is no easy way to do that you will need to step your way though it by getting the file, then getting its parent, followed by getting any more parents until you hit the top and build it up that way.

    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题