2016-07-19 1 views
0

파일을 업로드하고 Google 드라이브에 빈 본문이있는 파일이 있습니다. 왜 내가 구글 API 클라이언트Google 애플리케이션, Google 드라이브, 업로드 한 파일을 비운 후

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

를 설치

을 이해하고 내가 HWIO 번들하여 액세스 토큰 도움을받을 구글 DICS에 업로드 파일을 시도, 추가 범위

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,

/tmp/phpaPpHBp 

I 때문에 "이름"이 doc v3을 사용합니다 : 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": "" 

}

내가 파일에있는 모든이가 "구글/apiclient ","version ":"1.1.7 ""insert "기능 없음,

(210)

그래서, MIME_TYPE 올바른하지만 한 emty 내부에만이 같은 - 재개 미디어, 다중하지만 STIL 빈 시도 PDF 파일 MIME_TYPE = 응용 프로그램/PDF하지만 여전히 비어 있는게 내가 \ 무엇 '-/tmp를/phpaPpHBp 는 좀 uploadType 시도 잘못하고 왜 Google 드라이브에 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); 
    }