2016-06-15 67 views
1

下載的文件之前,我有下面的代碼通過谷歌的API(V2)Symfony3:在PHP谷歌API V3,而不是V2

public function downloadRoomAvailabilityAction() 
{ 
    $config = $this->container->getParameter('happy_r_google_api'); 
    $client = new GoogleClient($config); 
    $service = new \Google_Service_Drive($client->getGoogleClient()); 

    $file = $service->files->get('1iSAIgAsNjqqvzh05Pk4hos95VSgBW7JgLb1C3x_jA2M'); 

    $downloadUrl = $file->getExportLinks(); 

    if ($downloadUrl) { 
     $request = new \Google_Http_Request($downloadUrl['application/pdf'], 'GET', null, null); 

     $httpRequest = $service->getClient()->getAuth()->authenticatedRequest($request); 
     if ($httpRequest->getResponseHttpCode() == 200) { 

      $content = $httpRequest->getResponseBody(); 
      $response = new Response(); 

      $response->headers->set('Content-Type', 'application/pdf'); 
      $response->headers->set('Content-Disposition', 'attachment;filename="'.'room_availability.pdf'.'"'); 

      $response->setContent($content); 
      return $response; 
     } else { 
      // An error occurred. 
      return null; 
     } 
    } else { 
     // The file doesn't have any content stored on Drive. 
     return null; 
    } 
} 

但在這種不工作了V3下載PDF。因爲它給了我下面的錯誤:

Attempted to call an undefined method named "getExportLinks" of class "Google_Service_Drive_DriveFile". 

從實例說明文檔:

$fileId = '1ZdR3L3qP4Bkq8noWLJHSr_iBau0DNT4Kli4SxNc2YEo'; 
$content = $driveService->files->export($fileId, 'application/pdf', array('alt' => 'media')); 

,但我不知道如何修改我的現有代碼,使其與v3的工作。任何人都可以提出建議嗎? 我正在使用happy_r google symfony的api包,但如果有一種方法沒有它,那也沒有問題。

回答

1

你爲什麼不試試這個

public function downloadRoomAvailabilityAction() 
{ 
    $config = $this->container->getParameter('happy_r_google_api'); 
    $client = new GoogleClient($config); 
    $service = new \Google_Service_Drive($client->getGoogleClient()); 

    $fileId = '1iSAIgAsNjqqvzh05Pk4hos95VSgBW7JgLb1C3x_jA2M'; 
    $content = $service->files->export($fileId, 'application/pdf', array('alt' => 'media')); 

    if ($content) {    
     $response = new Response(); 

     $response->headers->set('Content-Type', 'application/pdf'); 
     $response->headers->set('Content-Disposition', 'attachment;filename="'.'room_availability.pdf'.'"'); 

     $response->setContent($content); 
     return $response;   
    } else { 
     // The file doesn't have any content stored on Drive. 
     return null; 
    } 
} 
+0

這並沒有爲我工作。我得到如下回應:{ 「錯誤」:{ 「錯誤」: { 「域」: 「全局」, 「理由」: 「InternalError該」 「消息」: 「內部錯誤」 } ], 「code」:500, 「message」:「Internal Error」 } } – apfz