Question

hi there i am using the following php code to upload files anonymously to google drive...

index.php file

    <?php
session_start();
$url_array = explode('?', 'http://'.$_SERVER ['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$url = $url_array[0];
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();
$client->setClientId('Client ID');
$client->setClientSecret('Secret key');
$client->setRedirectUri($url);
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
if (isset($_GET['code'])) {
    $_SESSION['accessToken'] = $client->authenticate($_GET['code']);
    header('location:'.$url);exit;
} elseif (!isset($_SESSION['accessToken'])) {
    $client->authenticate();
}
$files= array();
$dir = dir('files');
while ($file = $dir->read()) {
    if ($file != '.' && $file != '..') {
        $files[] = $file;
    }
}
$dir->close();
if (!empty($_POST)) {
    $client->setAccessToken($_SESSION['accessToken']);
    $service = new Google_DriveService($client);

    $file = new Google_DriveFile();
    foreach ($files as $file_name) {
        $file_path = 'files/'.$file_name;
        $mime_type = "text/plain";
        $file->setTitle($file_name);
        $file->setDescription('This is a '.$mime_type.' document');
        $file->setMimeType($mime_type);
        $createdFile = $service->files->insert(
            $file,
            array(
                'data' => file_get_contents($file_path),
                'mimeType' => $mime_type
            )
        );


        // Uncomment the following line to print the File ID
     //print 'File ID: %s' % $createdFile->getId();

    }

    header('location:'.$url);exit;
}
include 'index.phtml';
?>

and the index.phtml

<!DOCTYPE html>
<html lang="es">
    <head>
        <meta charset="UTF-8">
        <title>Google Drive Example App</title>
    </head>
    <body>
        <ul>
        <?php foreach ($files as $file) { ?>
            <li><?php echo $file; ?></li>
        <?php } ?>
        </ul>
        <form method="post" action="<?php echo $url; ?>">
            <input type="submit" value="Send" name="submit">
        </form>
    </body>
</html>

now the problem here is that every time i run the index.php on my web browser it asks me for authorization but what i am trying to do is let any random user with or without google drive accounts to upload files to my google drive account via the index.php. that does not seem to work.. what could i be doing wrong any help would be appreciated.. thanks

Was it helpful?

Solution

Despite the comments, you can allow anonymous users upload to your Google Drive account via your app.

One time only, you need to authenticate as yourself and then authorise your app to access your drive account using offline access. This will create a refresh token. Store that refresh token somewhere in your server. You can do this by writing some code that will only execute once, or you can use the Oauth2 Playground.

Then subsequently, an anonymous user can upload a file to your app, and then your app can upload that file to your Google Drive using the stored refresh token to request an access token.

An alternative (and possibly better) approach is to use a Service Account.

OTHER TIPS

found in the post link by @keaner ,it is possible
https://developers.google.com/drive/web/service-accounts

and regarding the service vs.regular account ,from the docs

"Because it is not possible to access the Google Drive web user interface of a service account, it is not possible to purchase additional Google Drive storage for this type of accounts. For that reason you may prefer using a regular account instead of a service account."

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top