Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将文件放入某个google drive帐户?_Php_Google Drive Api - Fatal编程技术网

Php 如何将文件放入某个google drive帐户?

Php 如何将文件放入某个google drive帐户?,php,google-drive-api,Php,Google Drive Api,我正在使用PHP将一些运行时创建的文件放在特定的Google Drive帐户上。 如果我用一个帐户登录谷歌,然后启动我的脚本,这些文件将被上传到这个帐户上,即使我使用不同帐户的客户ID和密码 用例: 我已经登录了a@gmail.com,我使用我的脚本并将文件发送到a@gmail.com的谷歌硬盘,而不是b@gmail.com谷歌硬盘 如何将这些文件上载到b@gmail.com帐户,即使我已登录a@gmail.com帐户 谢谢 前代码 <?php require_once 'goog

我正在使用PHP将一些运行时创建的文件放在特定的Google Drive帐户上。
如果我用一个帐户登录谷歌,然后启动我的脚本,这些文件将被上传到这个帐户上,即使我使用不同帐户的客户ID和密码

用例:
我已经登录了a@gmail.com,我使用我的脚本并将文件发送到a@gmail.com的谷歌硬盘,而不是b@gmail.com谷歌硬盘

如何将这些文件上载到b@gmail.com帐户,即使我已登录a@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;

您是否尝试过要与我们共享的内容?如果您想将内容上载到多个帐户,您可以尝试使用服务帐户。它适用于谷歌应用程序,但可能无法满足您的需求。您可能需要提供更多关于您的目标的信息:
<?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;