I am experiencing difficulties when trying to download files from Google Drive through the PHP API. Although the upload process to a particular folder is successful, accessing the downloadUrl from the returned file resource results in a 401 unauthorized error. The upload is successful, and I receive the file object, but the download URL does not work. Has anyone encountered a similar issue? What could I be overlooking in this process?
<?php
require_once 'vendor/autoload.php';
session_start();
$client_id = 'xxx';
$client_secret = 'yyy';
$redirect_uri = 'http://localhost/DriveTest/code.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
$authUrl = $client->createAuthUrl();
header("Location: $authUrl");
?>
And in code.php:
<?php
require_once 'vendor/autoload.php';
session_start();
$client_id = 'xxx';
$client_secret = 'yyy';
$redirect_uri = 'http://localhost/DriveTest/redirect.php';
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setApprovalPrompt('force');
$client->addScope("https://www.googleapis.com/auth/drive");
$service = new Google_Service_Drive($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
if ($client->isAccessTokenExpired()) {
unset($_SESSION['access_token']);
}
}
if ($client->getAccessToken()) {
$fileObject = new Google_Service_Drive_DriveFile();
$fileObject->setTitle("sample_file.exe");
$fileObject->setDescription("Sample file upload");
$fileObject->setMimeType('application/x-msdos-program');
$parentReference = new Google_Service_Drive_ParentReference();
$parentReference->setId("folder_id");
$fileObject->setParents(array($parentReference));
$fileContent = file_get_contents('sample_file.exe');
$uploadedFile = $service->files->insert($fileObject, array(
'data' => $fileContent,
'mimeType' => 'application/x-msdos-program',
'uploadType' => 'multipart',
'visibility' => 'DEFAULT'
));
header("Location: " . $uploadedFile->downloadUrl);
}
?>