【问题标题】:Google Apiclient, Google Drive, after uploaded file emptyGoogle Apiclient,Google Drive,上传文件后为空
【发布时间】:2016-07-19 19:03:14
【问题描述】:

我上传了一些格式的文件,并且在谷歌驱动器中有空正文的文件。为什么不明白

我安装了google api客户端

        "name": "google/apiclient",
        "version": "1.1.7",

并尝试在 google dics 中上传文件 我通过 HWIO 包获得访问令牌帮助,添加范围

scope:               
"https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/drive"

并像这样获取 accessToken

{"access_token":"TOKEN", "refresh_token":"TOKEN", "token_type":"Bearer",
 "expires_in":3600, "id_token":"TOKEN", "created":1320790426}

我尝试上传现有文件(doc、pdf、img)

    $client = $this->get('artel.google.api.client');

    /** @var CodeUserReference[] $accessToken */
    $accessToken = $this->get('doctrine.orm.entity_manager')->getRepository('ArtelProfileBundle:CodeUserReference')
        ->getReferenceAccessClient($user);
    if ($accessToken) {
        $client->setAccessToken($accessToken[0]->getAccessTokenClient());
    } else {
        $view = $this->view(['No accessToken was found for this user id'], 400);
        return $this->handleView($view);
    }
    $service = new \Google_Service_Drive($client->getGoogleClient());
    $file = new \Google_Service_Drive_DriveFile();
    $file->setName($request->request->get('title')
        ? $request->request->get('title')
        : $request->files->get('file')->getClientOriginalName()
    );
    $file->setDescription($request->request->get('description'));
    $file->setMimeType($request->request->get('mimeType')
        ? $request->request->get('mimeType')
        : $request->files->get('file')->getClientMimeType()
    );

    // Set the parent folder.Google_Service_Drive_ParentReferen, this class not find in google/apiclient, version": "1.1.7", I don\'t know why..(
    if ($request->request->get('parentId') != null) {
        $parent = new Google_Service_Drive_ParentReferen();
        $parent->setId($request->request->get('parentId'));
        $file->setParents(array($parent));
    }

    try {
        $data = $request->files->get('file');

        $createdFile = $service->files->create($file, array(
            'data' => $data,
            'mimeType' => $request->request->get('mimeType')
                ? $request->request->get('mimeType')
                : $request->files->get('file')->getClientMimeType(),
            'uploadType' => 'media'
        ));

        // Uncomment the following line to print the File ID
        // print 'File ID: %s' % $createdFile->getId();
        return View::create()
            ->setStatusCode(200)
            ->setData([$createdFile]);

    } catch (\Exception $e) {
        $view = $this->view((array) $e->getMessage(), 400);
        return $this->handleView($view);
    }

有回应

  {
"internal_gapi_mappings": [],
"model_data": [],
"processed": [],
"collection_key": "spaces",
"capabilities_type": "Google_Service_Drive_DriveFileCapabilities",
"capabilities_data_type": "",
"content_hints_type": "Google_Service_Drive_DriveFileContentHints",
"content_hints_data_type": "",
"id": "0B2_i_Tc5Vr8UWV9Jc2psQkhqS3M",
"image_media_metadata_type": "Google_Service_Drive_DriveFileImageMediaMetadata",
"image_media_metadata_data_type": "",
"kind": "drive#file",
"last_modifying_user_type": "Google_Service_Drive_User",
"last_modifying_user_data_type": "",
"mime_type": "application/msword",
"name": "Resume — Symfony Backend Developer, PHP, Shuba Ivan.doc",
"owners_type": "Google_Service_Drive_User",
"owners_data_type": "array",
"permissions_type": "Google_Service_Drive_Permission",
"permissions_data_type": "array",
"sharing_user_type": "Google_Service_Drive_User",
"sharing_user_data_type": "",
"video_media_metadata_type": "Google_Service_Drive_DriveFileVideoMediaMetadata",
"video_media_metadata_data_type": ""

}

我在文件中的所有内容:

/tmp/phpaPpHBp

我之所以使用这个doc v3,是因为在“名称”:“google/apiclient”、“版本”:“1.1.7”中找不到函数“插入”,

所以,mime_type 正确但在 emty 内部,只是这样 - /tmp/phpaPpHBp 我尝试了一些 uploadType - 媒体、多部分、可恢复但仍然为空 当尝试 pdf mime_type = application/pdf 但仍然是 emty 我做错了什么以及为什么我在谷歌驱动器中有 emty 文件?

谁知道,帮忙

【问题讨论】:

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


    【解决方案1】:

    添加 file_get_content 并正常工作:

        if ($request->request->get('parentId') !== null) {
            $parent = new \Google_Service_Drive_ParentReference();
            $parent->setId($request->request->get('parentId'));
            $file->setParents(array($parent));
        }
    
        $insertArray = [
            'mimeType' => isset($fileUpload) ? $fileUpload->getClientMimeType() :  $request->request->get('mimeType')
        ];
    
        if (isset($fileUpload)) {
            $insertArray ['uploadType'] = 'media';
            $insertArray ['data'] = file_get_contents($fileUpload);
        }
    
        try {
            $createdFile = $service->files->insert($file, $insertArray);
    
            return View::create()
                ->setStatusCode(200)
                ->setData([$createdFile]);
    
        } catch (\Exception $e) {
            $view = $this->view((array) self::ERROR_OCCURRED . $e->getMessage(), 400);
            return $this->handleView($view);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多