Symfony Google Drive Api - php

I used Symfony and I need create file in google drive and add access to write for another user to this file. I read google api drive
intsall google/apiclient
and run qiuck star, I get name for my share file. But I not find how to create new file and how to share this file for some user, this is possible ?
I find how to create file and add to quick start
crate file
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'Project plan',
'mimeType' => 'application/vnd.google-apps.drive-sdk'));
$file = $service->files->create($fileMetadata, array(
'fields' => 'id'));
printf("File ID: %s\n", $file->id);
but have error
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/drive/v3/files?fields=id: (403) Insufficient Permission' in /home/ivan/host/aog-code/vendor/google/apiclient/src/Google/Http/REST.php:110
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
how to correct add file and add to permission for some user to this file ?

Related

Google Cloud Storage bucket->exists gives 403

I am trying to check if a bucket exists in Laravel PHP.
I am getting a 403 on the exists() method. Why?
See line 160 https://github.com/googleapis/google-cloud-php/blob/master/Storage/src/Bucket.php
$storageClient = new \Google\Cloud\Storage\StorageClient([
'projectId' => env('GCS_PROJECT_ID'),
'keyFilePath' => storage_path(env('GCS_KEY_FILE')),
]);
$bucket = $storageClient->bucket('mybucketname');
if (!$bucket->exists()) {
$bucket = $storageClient->createBucket('mybucketname');
}
{ "error": {
"code": 403,
"message": "myaccount#api-project-xxxxxxxxx.iam.gserviceaccount.com does not have storage.buckets.get access to downloads.",
"errors": [ {
"message": "myaccount#api-project-xxxxxxxx.iam.gserviceaccount.com does not have storage.buckets.get access to mybucketname.",
"domain": "global",
"reason": "forbidden"
} ]
} }
Your service account does not have the storage.buckets.get permission. In order to check whether a bucket exists and then create it, assign roles/storage.admin to your service account.
For reference, see:
Cloud IAM roles for Cloud Storage
Cloud IAM permissions for Cloud Storage

How do i create permission on google sheet on docs in PHP

How do i create permission on google sheet on docs. Here it's show the error
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
here my code. i use php library.
function insertPermission($fileId) {
$client = $this->getClient();
$client->setScopes(Google_Service_Drive::DRIVE);
$service = new Google_Service_Drive($client);
$newPermission = new Google_Service_Drive_Permission(
array(
"role"=> "writer",
"type"=> "domain",)
);
try {
return $service->permissions->create($fileId, $newPermission);
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
return NULL;
}
After set permisson it's denied and return this error.
The permissions.create method can be used to insert permissions into a file on google drive.
POST https://www.googleapis.com/drive/v3/files/fileId/permissions
{
"role": "writer",
"type": "user",
"emailAddress": "test#test.com"
}
In order to run the above request the currently authenticated user must have been authenticated using the following scopes and of course have permissions on the file already
https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.file
insufficientPermissions
Means that the user you are authenticated with either hasn't granted your application permissions to make these changes or they themselves do not have the permissions. First thing i would do would be check the scopes your application has requested and add the scopes needed and re-authenticated the user and try again.

Error when creating folder in google drive

I have search in th enet but i can't find the solutions. Please help. Below is the code.
<?php
require_once '../../vendor/autoload.php';
define('APPLICATION_NAME', 'Drive API PHP Quickstart');
define('CREDENTIALS_PATH', '~/.credentials/drive-php-quickstart.json');
define('CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Drive::DRIVE_METADATA_READONLY)
));
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setScopes(array('https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.apps.readonly', 'https://www.googleapis.com/auth/drive'));
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$service = new Google_Service_Drive($client);
$fileMetadata = new Google_Service_Drive_DriveFile(array(
'name' => 'Test Folder',
'mimeType' => 'application/vnd.google-apps.folder'));
$file = $service->files->create($fileMetadata, array(
'fields' => 'id'));
printf("Folder ID: %s\n", $file->id);die();
?>
I can list all the files from the drive but I can't create a folder. What I'm missing? Below is the error. Can someone help me to solve this problem of mine. Thanks in advance.
PHP Fatal error: Uncaught Google_Service_Exception: {
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
I hope this will assist you in solving your problem, I found your error here:
https://developers.google.com/drive/v3/web/handle-errors
To help you navigate to a solution, here is the snippet, hope this assists you in some way:
401: Invalid Credentials
Invalid authorization header. The access token you're using is either expired or invalid.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization",
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
Suggested action: Refresh the access token using the long-lived refresh token. If this fails, direct the user through the OAuth flow, as described in Authorizing Your App with Google Drive.
If you want to know more about Authorizing your app with google drive, please refer to this site:
https://developers.google.com/drive/v3/web/about-auth
Hope you are able to find a solution with the resources provided.

Insufficient Permission

befor i ask i want to introduce my first steps.
I used this for example: PHP Quickstart
Install google/apiclient via composer
Create a project in Google Developers Console
Add credentials to my project
Upload client_secret.json to my webserver
Run the script & enter the code
Credentials saved to /files/php-yt-oauth2.json
This channel's ID is UC_x5XG1OV2P6uZZ5FSM9Ttw. Its title is Google Developers, and it has 125874726 views.
Now i want to list all videos of one playlist:
function playlistItemsListByPlaylistId($service, $part, $params) {
$params = array_filter($params);
$response = $service->playlistItems->listPlaylistItems(
$part,
$params
);
print_r($response);
}
playlistItemsListByPlaylistId($service,
'snippet',
array('maxResults' => 1, 'playlistId' => 'ID'));
An get the following error:
[23-Dec-2017 14:29:59 Europe/Berlin] PHP Fatal error: Uncaught Google_Service_Exception: {
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientPermissions",
"message": "Insufficient Permission"
}
],
"code": 403,
"message": "Insufficient Permission"
}
}
in /html/sa/vendor/google/apiclient/src/Google/Http/REST.php:118
I dont know whats going wrong and i hope someone can help me with this issue.

How to delete folder drive api

I am unable to delete a folder (created by another person) even if I try to change the rights, I have a console application and the current authenticated user can do the following :
create folders/files
move files
Scopes:
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.appdata',
'https://spreadsheets.google.com/feeds',
Tries:
1.Set permissions to the current user
public function deleteFolder(\Google_Service_Drive_DriveFile $folder)
{
$permission = new \Google_Service_Drive_Permission();
$permission->setRole( 'owner' );
$permission->setType( 'user' );
$permission->setEmailAddress('someId#developer.gserviceaccount.com');
$permission = $this->googleDriveClient->permissions->create( $folder->getId(),$permission,array('transferOwnership'=>true));
$this->googleDriveClient->files->delete($folder->getId());
}
Result:
[Google_Service_Exception] {
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error" } }
2.Set permission type to anyone
public function deleteFolder(\Google_Service_Drive_DriveFile $folder)
{
$permission = new \Google_Service_Drive_Permission();
$permission->setRole( 'owner' );
$permission->setType( 'anyone' );
$permission = $this->googleDriveClient->permissions->create( $folder->getId(),$permission,array('transferOwnership'=>true));
$this->googleDriveClient->files->delete($folder->getId());
}
Result:
[Google_Service_Exception]
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientFilePermissions",
"message": "The user does not have sufficient permissions for this file ."
}
],
"code": 403,
"message": "The user does not have sufficient permissions for this file." }
}
try 3: Impersonate the creator
$this->googleClient->setAccessType('offline');
$this->googleClient->setSubject('x#domain.com');//if removed everything works
[GuzzleHttp\Exception\ClientException]
Client error: POST https://www.googleapis.com/oauth2/v4/token
resulted in a 401 Unauthorized response:
{
"error": "unauthorized_client",
"error_description": "Unauthorized client or scope in request.",
"error_uri": ""
}
Am I missing something?
Only the owner of the folder can delete the folder.
Use a service account Using OAuth 2.0 for Server to Server Applications, take the identity of the owner of the folder Perform Google Apps Domain-Wide Delegation of Authority.
Once authorized as the owner of the folder it should be possible to delete the folder.

Categories