0

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

我想做的是让移动客户端将文件上传到 API,然后将该文件上传到 Google Drive。我查看了 Google Drive API,并成功地从我的家用计算机上传了一个文件,但为了做到这一点,我需要登录我的谷歌帐户以授权上传。这是代码:

<?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。

任何帮助将不胜感激!

4

1 回答 1

0

解决方案是使用使用服务帐户或常规帐户的应用程序拥有的帐户。请按照https://developers.google.com/drive/web/service-accounts上的说明进行操作。

于 2014-09-04T11:37:37.747 回答