基于服务器的Google Drive服务?

时间:2014-01-17 19:56:37

标签: php google-drive-api google-oauth google-api-php-client

我希望构建一个Google Drive应用程序来存储来自网站应用程序的图像和文档文件,而无需访问者进行身份验证或提供访问权限等。通过PHP界面无缝使用googledrive服务。

用户将图像/文档上传到网络表单,文件存储在GoogleDrive而不是本地文件系统中。

我只是需要帮助,因为我使用的所有Google客户端示例都需要基于用户的授权和权限。

我现在的代码似乎让我得到了我的Google_DriveService数组:

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;

我返回了一大堆物体,从那里,我更像是石墙。

其中一些看起来像:

[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

1 个答案:

答案 0 :(得分:0)

我认为您应该考虑使用服务帐户。

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

通过使用服务帐户,您不必担心人们登录.Everthing将始终上传到同一服务帐户。