w3lifer / php-google-drive
1.0.1
2024-09-01 11:09 UTC
Requires
- php: ^7.0|^8.0
- google/apiclient: ^2.0
This package is auto-updated.
Last update: 2024-12-26 18:37:29 UTC
README
Installation
composer require w3lifer/php-google-drive
Usage
<?php // require_once __DIR__ . '/vendor/autoload.php'; use w3lifer\google\Drive; $googleDrive = new Drive([ 'pathToCredentials' => __DIR__ . '/credentials.json', // Required 'pathToToken' => __DIR__ . '/token.json', // Required ]); $fileId = $googleDrive->upload( __DIR__ . '/hello.txt', // Required [ // Optional '<folder id>', '<folder id>', ] );
- Folder ID:
https://drive.google.com/drive/folders/<folder-id>
How to get Google Drive API credentials
Create a new project
Enable Google Drive API
Create credentials
If you left the screen above, go to "Enable API & services" and click "Google Drive API":
Publish app
How to get a token
1. Save the credential to disk and specify the path to them by setting the pathToCredentials
configuration key
$googleDrive = new Drive([ 'pathToCredentials' => __DIR__ . '/credentials.json', // Required 'pathToToken' => __DIR__ . '/token.json', // Required ]);
Note that
pathToToken
is the path where theDrive
saves the token after the first run. That is, the token will be saved automatically along the specified path; it is needed for your application to subsequently access Google Drive without the consent screen.
2. Run your app
- You will receive the following message in the console:
Open the following link in your browser:
https://accounts.google.com/o/oauth2/v2/auth?...
Enter verification code:
- Open the link in the message and choose desired account:
- The warning "Google hasn't verified this app" may appear. Click "Advanced" and then "Go to ... (unsafe)":
- On the next screen click "Continue":
- After the redirect, copy the code from the address bar of your browser and paste it in the console:
Open the following link in your browser:
https://accounts.google.com/o/oauth2/v2/auth?...
Enter verification code: <here>
3. Enjoy!