0

I am looking to build a Google Drive application to store image and document files from a website application, without the requirement of a visitor authenticating, or providing permission to access, etc. Seamless use of a googledrive service through a PHP interface.

Users will upload images/documents to a web form, and the files are stored on a GoogleDrive instead of local file system.

I'm just in need of assistance with this, as all the Google Client examples I've worked with require user based authorization, and permissions.

The code I have now seems to get me my Google_DriveService array:

require_once '../apis/google-api-php-client/src/Google_Client.php';
require_once '../apis/google-api-php-client/src/contrib/Google_PredictionService.php';
require '../apis/google-api-php-client/src/contrib/Google_DriveService.php';

session_start();

// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accounts
const CLIENT_ID = '########.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = '#########@developer.gserviceaccount.com';

// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = '##########-privatekey.p12';

// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client = new Google_Client();

$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
    SERVICE_ACCOUNT_NAME,
    array('https://www.googleapis.com/auth/prediction'),$key));

$client->setClientId(CLIENT_ID);
$service = new Google_PredictionService($client);

// Prediction logic:
$id = '9146497114232; # replaced with random numbers for this post
$hostedModelName= 'sample.languageid';
$predictionData = new Google_InputInput();
$predictionData->setCsvInstance(array('Foo Bar A Doo')); ## Not sure what this is or what I need here?
$input = new Google_Input();
$input->setInput($predictionData);

$result = $service->hostedmodels->predict($id,$hostedModelName, $input);

# print '<h2>Prediction Result:</h2><pre>' . print_r($result, true) . '</pre>';

// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
if ($client->getAccessToken()) {
    $_SESSION['token'] = $client->getAccessToken();
}

$driveService = new Google_DriveService($client);


print_r($driveService);
exit;

I am returned a lengthy array of objects, and from there, I'm rather stone-walled.

Some of which looks like :

[service:Google_ServiceResource:private] => Google_DriveService Object
 *RECURSION*
            [serviceName:Google_ServiceResource:private] => drive
            [resourceName:Google_ServiceResource:private] => revisions
            [methods:Google_ServiceResource:private] => Array
                (
                    [delete] => Array
                        (
                            [id] => drive.revisions.delete
                            [path] => files/{fileId}/revisions/{revisionId}
                            [httpMethod] => DELETE
                            [parameters] => Array
                                (
                                    [fileId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                    [revisionId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                )

                            [scopes] => Array
                                (
                                    [0] => https://www.googleapis.com/auth/drive
                                    [1] => https://www.googleapis.com/auth/drive.file
                                )

                        )

                    [get] => Array
                        (
                            [id] => drive.revisions.get
                            [path] => files/{fileId}/revisions/{revisionId}
                            [httpMethod] => GET
                            [parameters] => Array
                                (
                                    [fileId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                    [revisionId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                )

                            [response] => Array
                                (
                                    [$ref] => Revision
                                )

                            [scopes] => Array
                                (
                                    [0] => https://www.googleapis.com/auth/drive
                                    [1] => https://www.googleapis.com/auth/drive.file
                                    [2] => https://www.googleapis.com/auth/drive.metadata.readonly
                                    [3] => https://www.googleapis.com/auth/drive.readonly
                                )

                        )

                    [list] => Array
                        (
                            [id] => drive.revisions.list
                            [path] => files/{fileId}/revisions
                            [httpMethod] => GET
                            [parameters] => Array
                                (
                                    [fileId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                )

                            [response] => Array
                                (
                                    [$ref] => RevisionList
                                )

                            [scopes] => Array
                                (
                                    [0] => https://www.googleapis.com/auth/drive
                                    [1] => https://www.googleapis.com/auth/drive.file
                                    [2] => https://www.googleapis.com/auth/drive.metadata.readonly
                                    [3] => https://www.googleapis.com/auth/drive.readonly
                                )

                        )

                    [patch] => Array
                        (
                            [id] => drive.revisions.patch
                            [path] => files/{fileId}/revisions/{revisionId}
                            [httpMethod] => PATCH
                            [parameters] => Array
                                (
                                    [fileId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                    [revisionId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                )

                            [request] => Array
                                (
                                    [$ref] => Revision
                                )

                            [response] => Array
                                (
                                    [$ref] => Revision
                                )

                            [scopes] => Array
                                (
                                    [0] => https://www.googleapis.com/auth/drive
                                    [1] => https://www.googleapis.com/auth/drive.file
                                )

                        )

                    [update] => Array
                        (
                            [id] => drive.revisions.update
                            [path] => files/{fileId}/revisions/{revisionId}
                            [httpMethod] => PUT
                            [parameters] => Array
                                (
                                    [fileId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                    [revisionId] => Array
                                        (
                                            [type] => string
                                            [required] => 1
                                            [location] => path
                                        )

                                )

                            [request] => Array
                                (
                                    [$ref] => Revision
                                )

                            [response] => Array
                                (
                                    [$ref] => Revision
                                )

                            [scopes] => Array
                                (
                                    [0] => https://www.googleapis.com/auth/drive
                                    [1] => https://www.googleapis.com/auth/drive.file
                                )

                        )

                )

        )

    [version] => v2
    [servicePath] => drive/v2/
    [resource] => 
    [serviceName] => drive
pinoyyid
  • 21,499
  • 14
  • 64
  • 115
Scott Fleming
  • 452
  • 6
  • 14
  • 2
    Whose Google Drive will the app be uploading to - yours? – Pekka Jan 17 '14 at 20:00
  • 2
    I am a little confused. Do your users currently log into your PHP site? If so then can you hard-code the credentials for the [Google Drive API quickstart](https://developers.google.com/drive/quickstart-php) and [DrEdit](https://developers.google.com/drive/examples/php) so that all users just use that connection? – MonkeyZeus Jan 17 '14 at 20:08
  • 1
    Yes, users will be logged in to the application, but we want to use a single Google Drive account for storage of the images and documents, not the users' drive. – Scott Fleming Jan 17 '14 at 21:12
  • poss duplicate of http://stackoverflow.com/questions/21189523/how-upload-to-my-google-drive-account-not-the-users/21192890#21192890 . The question may be different, but the answer is the same ;-) – pinoyyid Jan 18 '14 at 09:54

1 Answers1

0

I think you should look into using a service acccount for this.

https://developers.google.com/drive/web/service-accounts

By using a service account you wont have to worry about people loging in. Everthing will always be uploaded to the same service account.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449