谷歌硬盘上传PHP编程登录


Google Drive upload PHP programmatic sign in

我正在开发一个需要存储文件的移动应用程序的web API。目前,移动客户端使用文件的数据向API发送请求,文件直接存储在服务器上。然而,我需要节省服务器上的空间,这只是一个临时的解决方案。

我想做的是让移动客户端将文件上传到API,然后将该文件上传到Google Drive。我已经看了一下Google Drive API,我成功地从我的家用电脑上上传了一个文件,但为了做到这一点,我需要登录我的Google帐户来授权上传。下面是代码:

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the console
$client->setClientId(' my client id ');
$client->setClientSecret(' my client secret ');
$client->setRedirectUri('https://www.hwassassin.net16.net/oauth2callback');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$authUrl = $client->createAuthUrl();
//Request authorization
print "Please visit:'n$authUrl'n'n";
print "Please enter the auth code:'n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);
//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');
$data = file_get_contents('document.txt');
$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));
print_r($createdFile);
?>

由于我需要从后端上传文件,我想知道我是否可以以某种方式从服务器编程登录到我的Google Drive帐户,而不需要客户端采取任何行动。

如果这是不可能的,我将欣赏任何替代的云存储API,不需要客户端登录到一个帐户。

任何帮助都将非常感激!

解决方案使用应用程序拥有的帐户使用服务帐户或常规帐户。