I'm trying to upload a 50MB file upload to a folder on my Google Drive account.
Algorithm:
Then file should be available on my Google Drive as blahblahblah/backup.sql.tar.gz
But it isn't. And I'm sure that everything fails on part where upload is ended - no output.
I don't even know how the hell I'm supposed to get client secret? I tried dev console but I only get:
CLIENT ID
CERTIFICATE FINGERPRINTS
API KEY
I have tried so far:
https://developers.google.com/drive/web/quickstart/quickstart-php https://github.com/google/google-api-php-client/tree/master/examples
And also this:
<?PHP
/**
* Insert new file.
*
* @param Google_DriveService $service Drive API service instance.
* @param string $title Title of the file to insert, including the extension.
* @param string $description Description of the file to insert.
* @param string $parentId Parent folder's ID.
* @param string $mimeType MIME type of the file to insert.
* @param string $filename Filename of the file to insert.
* @return Google_DriveFile The file that was inserted. NULL is returned if an API error occurred.
*/
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
$file = new Google_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
// Set the parent folder.
if ($parentId != null) {
$parent = new Google_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
try {
$data = file_get_contents($filename);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
'uploadType' => 'media'
));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
return $createdFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}
I use the latest Google API PHP Client Library.
Can I get simple PHP code which will work out of the box utilizing the API and uploading the file?
There are no user actions involved. I do not need to authorize user. Everything should be done on server side.
Client Secret is for Web applications, where multiple or single users have to sign in to give authorization. Your case needs a service account, where a single account handles everything and only needs certain parameters that can be coded in your scripts.
$json = json_decode(file_get_contents("xxxxxx.json"), true);
$CLIENT_ID = $json['client_id'];
$CLIENT_EMAIL = $json['client_email'];
$REDIRECT_URI = "";
$private_key = file_get_contents('xxxxxxxx.p12');
$credentials = new Google_Auth_AssertionCredentials(
$CLIENT_SECRET,
$SCOPES,
$private_key
);
$client = new Google_Client();
$client->setApplicationName("appName");
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}
Fill in the x's with your files, make sure to download the .json and .p12 so the script can read it. Hope it helped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With