我怎么能把文件放到某个谷歌驱动器帐户?

时间:2013-03-05 11:24:05

标签: php google-drive-api

我正在使用PHP在特定的Google云端硬盘帐户上放置一些运行时创建的文件 如果我使用帐户登录谷歌并启动我的脚本,这些文件将上传到此帐户,即使我使用客户ID和其他帐户的秘密

用例:
我用a@gmail.com登录,我使用我的脚本,文件被发送到a@gmail.com的Google Drive而不是b@gmail.com的Google Drive

即使我使用a@gmail.com帐户登录,如何将这些文件上传到b@gmail.com帐户?

由于

前。代码

    <?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$drive = new Google_Client();

$drive->setClientId(/*client id*/);
$drive->setClientSecret(/*client secret*/);
$drive->setRedirectUri(/*redirect uri (the same URI in the API configuration)*/); 
$drive->setScopes(array('https://www.googleapis.com/auth/drive'));

$gdrive = new Google_DriveService($drive);

file_put_contents('token.json', $drive->authenticate());

$drive->setAccessToken(file_get_contents('token.json'));

$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');


$createdFile = $service->files->insert($file, array(
  'data' => 'example',
  'mimeType' => 'text/plain',
));


return true;

编辑:

我使用路径查找器解决方案并创建一个服务帐户。 它似乎工作但我仍然有一个问题:在执行结束时我无法在gDrive上找到我的文件...也许我已经完成了一个完整的配置,即使我阅读了文档......

这是代码:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
$DRIVE_SCOPE = 'https://www.googleapis.com/auth/drive';
$SERVICE_ACCOUNT_EMAIL = '<email_in_api_access>@developer.gserviceaccount.com';
$SERVICE_ACCOUNT_PKCS12_FILE_PATH = 'google-api-phpclient/<api_key_file>-privatekey.p12';

$key = file_get_contents($SERVICE_ACCOUNT_PKCS12_FILE_PATH);
$auth = new Google_AssertionCredentials(
$SERVICE_ACCOUNT_EMAIL,
array($DRIVE_SCOPE),
$key);
$client = new Google_Client();
$client->setUseObjects(true);
$client->setAssertionCredentials($auth);
$gdrive = new Google_DriveService($client);
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$createdFile = $gdrive->files->insert($file, array(
'data' => 'A test document',
'mimeType' => 'text/plain',
));
return true;

0 个答案:

没有答案