2016-07-19 190 views
0

我上傳文件的一些格式,並在谷歌驅動器有空文件的文件。爲什麼不明白谷歌apiclient,谷歌驅動器,上傳文件後空

我安裝谷歌的API客戶端

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

,並嘗試在谷歌DICS上傳文件 我得到HWIO束訪問令牌的幫助下,添加範圍

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

,並得到accesToken這樣

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

beign我嘗試上傳存在文件(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:「谷歌/ apiclient「,」版本「:」1.1.7「找不到函數」插入「,

所以,MIME_TYPE正確的,但裏面emty,只有這樣的 - 的/ tmp/phpaPpHBp 我嘗試一些uploadType - 媒體多,可恢復但還是老樣子空 當嘗試PDF MIME_TYPE =應用程序/ PDF格式,但仍emty I \什麼我做錯了,爲什麼我在谷歌驅動器中有emty文件?

任何人,誰知道,幫

回答

0

附加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); 
    }