Creating and sharing files in Google Drive using Symfony

Hey everyone! I’m trying to figure out how to work with Google Drive in my Symfony project. I want to create a new file and then give another user write access to it. I’ve installed the Google API client and got the quick start example working, but I’m stuck on the next steps.

I tried to create a file like this:

$fileMetadata = new Google_Service_Drive_DriveFile([
    'name' => 'My Project Plan',
    'mimeType' => 'application/vnd.google-apps.document'
]);
$file = $service->files->create($fileMetadata, [
    'fields' => 'id'
]);
echo 'File ID: ' . $file->id;

But I’m getting a 403 error saying I have insufficient permissions. Any ideas on what I’m doing wrong? Also, once I get the file created, how can I share it with another user?

I’d really appreciate any help or examples you can provide. Thanks!

I encountered a similar issue when implementing Google Drive integration in a Symfony project. The 403 error usually indicates an authentication problem. Make sure you’ve correctly set up OAuth 2.0 and have the necessary scopes enabled for your application.

For file creation, double-check that you’re using the correct credentials and that the Drive API is enabled in your Google Cloud Console. Also, ensure you’re passing the access token properly to the Google_Client.

Regarding sharing, once you’ve successfully created the file, you can use the permissions.create method to grant access to another user. Here’s a basic example:

$newPermission = new Google_Service_Drive_Permission([
    'type' => 'user',
    'role' => 'writer',
    'emailAddress' => '[email protected]'
]);
$service->permissions->create($fileId, $newPermission);

Replace ‘[email protected]’ with the actual email of the user you want to share with. This grants them write access to the specific file.

I’ve dealt with Google Drive integration in Symfony before, and it can be tricky. For the 403 error, double-check your OAuth setup. Make sure you’ve got the right scopes, especially ‘https://www.googleapis.com/auth/drive.file’ for creating files.

One thing that caught me out was refresh tokens. If you’re using a service account, you might need to force a token refresh before making API calls. Try something like:

$client->fetchAccessTokenWithAssertion();

For sharing, I found it easier to use the ‘anyone with the link’ option instead of specific email addresses. You can do this with:

$permission = new Google_Service_Drive_Permission([‘type’ => ‘anyone’, ‘role’ => ‘writer’]);
$service->permissions->create($fileId, $permission);

Then just send the file link to whoever needs access. It’s less secure, but much simpler to implement. Just be careful with sensitive data!

yo, ive had similar probs with google drive. make sure ur oauth is set up right and u got the right scopes. for the 403, check ur credentials and if the api is turned on in google cloud console.

for sharing, try this after u make the file:

$newPerm = new Google_Service_Drive_Permission([‘type’ => ‘user’, ‘role’ => ‘writer’, ‘emailAddress’ => ‘[email protected]’]);
$service->permissions->create($fileId, $newPerm);

hope that helps!