I am developing a web API for a mobile app which needs to store files. Currently, the mobile clients send a request to the API with the file's data, and the file is stored directly on the server. However, I need to save space on the server and this is just a temporary solution.
What I would like to do is have the mobile client upload the file to the API, which then uploads that file to Google Drive. I have taken a look at the Google Drive API and I got it to upload a file from my home computer successfully, but in order to do so, I needed to sign in to my google account to authorize the upload. Here is the code:
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId(' my client id ');
$client->setClientSecret(' my client secret ');
$client->setRedirectUri('https://www.hwassassin.net16.net/oauth2callback');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:
$authUrl
";
print "Please enter the auth code:
";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt');
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => 'text/plain',
));
print_r($createdFile);
?>
Since I need to upload the file from the backend, I would like to know if I can somehow programmatically log in to my Google Drive account from the server, without requiring the Client to take any action.
If this is not even possible, I would appreciate any alternative cloud storage API's that don't require the client to sign in to an account.
Any help would be greatly appreciated!