How to shared file in Google Drive API v3 in php? - google-drive-api

How to share file in Google Drive API v3?I have listed of file and I want to add share functionality with Laravel?

If you mean sharing a file with another user, then you can do the following
client = new Google_Client();
// setup the client the way you do
// ....
service = new Google_Service_Drive(client)
$role = 'writer';
$userEmail = 'user#gmail.com';
$fileId = 'The ID of the file to be shared';
$userPermission = new Google_Service_Drive_Permission(array(
'type' => 'user',
'role' => $role,
'emailAddress' => $userEmail
));
$request = $service->permissions->create(
$fileId, $userPermission, array('fields' => 'id')
);
Reference:
https://developers.google.com/drive/v3/web/manage-sharing
Check my git repo for more useful examples
https://github.com/sazaamout/gDrive/blob/master/gDrive.php

Related

Google Drive API can't set permissions with reusable upload

When I did a basic file upload & set the permission like so, it worked great:
$file = $service->files->create($fileMetadata, array(
'data' => $content,
'mimeType' => $mimeType,
'uploadType' => 'multipart',
'fields' => 'id'));
$permissions = new Google_Service_Drive_Permission(array(
"role" => "reader",
"type" => "anyone",
));
$setPermission = $service->permissions->create($file->id, $permissions);
But when I upload a large file that's split into chunks & use the resumable uploadType, the permissions aren't set on the file & I don't get any errors:
$service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$file->title = $fileName;
$file->name = $fileName;
$chunkSizeBytes = 1 * 1024 * 1024;
$mimeType = mime_content_type($fullpath);
// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->create($file);
// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
$client,
$request,
$mimeType,
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize($fullpath));
// Upload the various chunks. $status will be false until the process is complete.
$status = false;
$handle = fopen($fullpath, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
// The final value of $status will be the data from the API for the object that has been uploaded.
$result = false;
if($status != false) {
$result = $status;
error_log($result->id);
}
fclose($handle);
$permissions = new Google_Service_Drive_Permission(array(
"role" => "reader",
"type" => "anyone"
));
$setPermission = $service->permissions->create($result->id, $permissions);
Any suggestions on what I'm doing wrong?
I have had the same situation with you. In that case, when the file information is retrieved with $res = $service->files->get($result->id); after the file was uploaded with the resumable upload, the empty object of {} is returned without error. I thought that this is the reason of the issue.
So, I used the following workaround. In this workaround, after the file was uploaded with the resumable upload, I retrieved $client again. By this, $res = $service->files->get($result->id); worked after the file was uploaded. When this is used for your script, please modify as follows.
From:
fclose($handle);
$permissions = new Google_Service_Drive_Permission(array(
"role" => "reader",
"type" => "anyone"
));
$setPermission = $service->permissions->create($result->id, $permissions);
To:
fclose($handle);
$client = getClient(); // <--- Added
$service = new Google_Service_Drive($client); // <--- Added
$permissions = new Google_Service_Drive_Permission(array(
"role" => "reader",
"type" => "anyone"
));
$setPermission = $service->permissions->create($result->id, $permissions);
getClient() is for retrieving $client. So please modify this part for your actual situation.
Note:
This answer supposes that your $client can be created the permissions to the uploaded file. Please be careful this.
$client->setDefer(true); is causing the issue. That's why it works when you create a new instance of Google client.
Another fix might be to do $client->setDefer(false); after uploading the file.

The "pageSize" parameter does not work in google drive api v3

I use google drive api v3 with php. I stuck with the one problem. I set pageSize to 1000, but I receive only a bit more than 300 files and nextPageToken. Why I have to use nextPageToken if I have only 400 files and set pageSize to 1000?
$drive = new Google_Service_Drive($client);
$optParams = array(
'pageSize' => 1000,
'fields' => "nextPageToken, files(id,name,mimeType,webContentLink,webViewLink)",
'q' => "'" . $folderId . "' in parents and trashed = false",
'orderBy' => 'name'
);
$results = $drive->files->listFiles($optParams);
There are various conditions that will cause the output set to be restricted to less than the proposed page size. For instance, including permissions in the files fields will limit the set to 100 each, while including parents will limit it to 360 items each. There are possibly other conditions as well.
Bottom line, you can not reliably depend on having the maximum output set at the size requested using pageSize. To insure that you get the complete set of files requested, you will need to check and process the nextPageToken.
Here is an example:
function GetFiles($service)
{
$fileFields =
'id, mimeType, name, ownedByMe, owners, parents, webContentLink';
$options =
[
'pageSize' => 1000,
'supportsAllDrives' => true,
'fields' => "files($fileFields), nextPageToken"
];
$files = [];
$pageToken = null;
do
{
try
{
if ($pageToken !== null)
{
$options['pageToken'] = $pageToken;
}
$response = $service->files->listFiles($options);
$files = array_merge($files, $response->files);
$pageToken = $response->getNextPageToken();
}
catch (Exception $exception)
{
$message = $exception->getMessage();
echo "exception: $message\r\n";
$pageToken = null;
}
} while ($pageToken !== null);
return $files;
}

add folder to folder using google drive API

The script below is creating folders within a parent folder using
"google drive API". It works perfectly however, after a while (20 folders or so) it is not working anymore.
No error message just no more folder creation within the parent folder.
It goes somewhere else!
To enable that creation a "service account" was created and parent folder is share between "personal google account" and "service account"
Can someone provide help please?
php function send_google_drive($id,$fileno,$filename1,$filename2){
global $wpdb;
require(ABSPATH.'/wp-content/themes/enemat/googledrives/vendor/autoload.php');
$client = getClient();
$service = new Google_Service_Drive($client);
if(!empty($filename1)){
$results = $service->files->listFiles();
foreach ($results->getFiles() as $item) {
if ($item['name'] == 'ENEMAT CRM FILES') {
$folderId = $item['id'];
break;
}
}
$parentid = $folderId;
$childid = "";
foreach ($results->getFiles() as $item) {
if ($item['name'] == $fileno) {
$childid = $item['id'];
break;
}
}
if(empty($childid)){
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => $fileno,
'parents'=>array($parentid),
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array(
'fields' => 'id'));
$folderId = $file->id;
}else{
$folderId = $childid;
}
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($folderId, $newPermission);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => array(basename($filename1)),
'parents' => array($folderId)
));
$content = file_get_contents($filename1);
$files = $service->files->create($fileMetadata, array(
'data' => $content,
'uploadType' => 'resumable',
'fields' => 'id'));
$fileids = $files->id;
$docusignorgs = "https://drive.google.com/open?id=".$fileids."";
$folderslink = "https://drive.google.com/drive/folders/".$folderId."";
#unlink(ABSPATH."wp-content/themes/enemat/pdfs/".basename($filename1));
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($fileids, $newPermission);
}
if(!empty($filename2)){
$results = $service->files->listFiles();
foreach ($results->getFiles() as $item) {
if ($item['name'] == '46 - CONTRAT PARTENARIAT') {
$folderId = $item['id'];
break;
}
}
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => array(basename($filename2)),
'parents' => array($folderId)
));
$content = file_get_contents($filename2);
$files = $service->files->create($fileMetadata, array(
'data' => $content,
'uploadType' => 'resumable',
'fields' => 'id'));
$fileids1 = $files->id;
$contractdrivelink = "https://drive.google.com/open?id=".$fileids1."";
$newPermission = new Google_Service_Drive_Permission();
$newPermission->setType('anyone');
$newPermission->setRole('reader');
$service->permissions->create($fileids1, $newPermission);
}
}
?
The reason there is no error message is because your code has no error handling! If GDrive fails to do something, it returns an error code and message to explain why. Your code should be catching that error and displaying it.
My guess is that you are hitting a rate limit. To see if this is the cause or not, add a 2 second delay between each folder creation. If it no runs correctly, you know that rate limiting is your problem.

Googledrive API Upload file via PHP return Error

Have Problem with Upload any file to Google Drive Cloud via PHP,
Another Process Listing files, Children Listen, File details fork fine.
$client = new Google_Client();
$client->setApplicationName("File Upload Testing");
$client->setClientId('myID');
$client->setClientSecret('mySecret');
$client->setScopes(
array(
"https://www.googleapis.com/auth/drive",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive.appdata",
"https://www.googleapis.com/auth/drive.apps.readonly",
));
$parent = new Google_Service_Drive_ParentReference();
$parent->setId("0B1OLO5r_T5znT3dRTUdNNGVxdms");
$file = new Google_Service_Drive_DriveFile();
$file->setParents(array($parent));
$file->setTitle("File.png");
$file->setDescription("File pnd desc");
$file->setMimeType("image/png");
$content = file_get_contents('images/resim.jpg');
try{
$file = $service->files->insert($file, array(
'data' => $content,
'mimeType' => 'image/png',
'uploadType' => 'multipart',
'fields' => 'id'));
printf("File ID: %s\n", $file->id);
} catch( Execution $e){
echo $e->getMessage();
}
image in correct path!, wenn i remove data from array file than, upload process successfully but empty not content, with data param, return always 404 not Found!,
have couldn't found this problem, have any idea?

HybridAuth CakePHP3.X,how to save user after successful login?

I have read the description over,
Once a user is authenticated through the provider the authenticator gets the user profile from the identity provider and using that tries to find the corresponding user record in your app's users table. If no user is found and registrationCallback option is specified the specified method from the User model is called. You can use the callback to save user record to database.
But where to define/declare registrationCallback
If you want user to register if not exist then this code will execute :
if (!empty($this->_config['registrationCallback'])) {
$return = call_user_func_array(
[
TableRegistry::get($userModel),
$this->_config['registrationCallback']
],
[$provider, $providerProfile]
);
if ($return) {
$user = $this->_fetchUserFromDb($conditions);
if ($user) {
return $user;
}
}
You need to define the registration function in config ( in __construct) and regarding call_user_func_array read this link - https://php.net/call-user-func-array
To Store user in database after login
1>defines the function in UsersTabel.php
public function registration($provider, $profile) {
$user = $this->newEntity([
'username' => $profile->displayName,
'provider' => $provider,
'provider_uid' => $profile->identifier
]);
if(!$this->save($user))
{
Log::write(LOG_ERR, 'Failed to create new user record');
return false;
}
return true;
}
2>Replace the function of file vendor ▸ admad ▸ cakephp-hybridauth ▸ src ▸ Auth▸ HybridAuthAuthenticate.php
public function __construct(ComponentRegistry $registry, $config)
{
$this->config([
'fields' => [
'provider' => 'provider',
'provider_uid' => 'provider_uid',
'openid_identifier' => 'openid_identifier'
],
'hauth_return_to' => null,
'registrationCallback'=>'registration'
]);
parent::__construct($registry, $config);
}