我愿意通过 FlySystem 管理 Google Drive 文件夹。我通过官方客户端库找到了大量有关通过 PHP 访问 Google Drive 的信息。
但我发现只有 2 个场景我可以看到,我需要第三个场景:
[email protected]
,该用户仅由办公室老板管理。[email protected]
有一个与所有员工共享的文件夹。[email protected]
凭证。[email protected]
中的该文件夹。我所看到的:
[email protected]
[email protected]
通过标准驱动器 Web-UI 轻松查看所有文件。PD:我的最终目标是使用flysystem的驱动,虽然问题与flysystem本身没有直接关系,但驱动访问。
找到了。
长话短说,这不是关于拥有服务帐户。这是关于在“离线模式”下使用 OAuth2。
这就是窍门:
这些是获取所需数据的步骤。
在此处找到获取客户端 ID 和密钥的说明以及在此处获取 reresh 令牌。
这是我的示例代码,使用了所有这些内容,经过测试并允许从一个 gmail 帐户(包含应用程序)访问另一个不同的、固定始终相同的帐户(包含数据)。
在此示例中,我创建了一个由
League\Flysystem\Filesystem
驱动的标准跨驱动程序 Google\Client
对象,其配置方式使其可以从给定的特定 google 驱动器访问文件,而无需用户交互(后台/服务器使用)。
use Google\Client as GoogleClient;
use Google\Service\Drive;
use League\Flysystem\Config;
use League\Flysystem\Filesystem;
use League\Flysystem\Visibility;
// Define the class, etc. Next is a single method.
// Inject $this->googleClientConfig via configuration-injection.
private getGoogleDriveFilesystem() : Filesystem
{
$config = [
'client_id' => $this->googleClientConfig[ 'clientId' ],
'client_secret' => $this->googleClientConfig[ 'clientSecret' ],
'application_name' => 'My Nice App Sandbox FlySystem Test',
];
// This consumes the standard PHP Google Client.
$googleClient = new GoogleClient( $config );
// This is the key to skip the user-interaction.
$googleClient->fetchAccessTokenWithRefreshToken( $this->googleClientConfig[ 'userRefreshToken' ] );
$drive = new Drive( $googleClient );
$adapter = new GoogleDriveAdapter(
$drive,
$this->googleClientConfig[ 'userRootFolder' ],
);
$filesystem = new Filesystem( $adapter, [ Config::OPTION_VISIBILITY => Visibility::PRIVATE ] );
return $filesystem;
}