Symfony3: скачивайте файлы в php google api v3 вместо v2

Ранее у меня был следующий код для загрузки PDF через Google API (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 это больше не работает. как это дает мне следующую ошибку:

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 api bundle для Symfony, хотя, если есть способ сделать это без него, это тоже хорошо.

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;
    }
}
Другие вопросы по тегам