0

ファイルを保存する必要があるモバイル アプリ用の Web API を開発しています。現在、モバイル クライアントはファイルのデータを含むリクエストを API に送信し、ファイルはサーバーに直接保存されます。ただし、サーバーのスペースを節約する必要があり、これは一時的な解決策にすぎません。

私がやりたいことは、モバイル クライアントがファイルを API にアップロードし、API がそのファイルを Google ドライブにアップロードすることです。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 ドライブ アカウントにログインできるかどうかを知りたいです。

これが不可能な場合でも、クライアントがアカウントにサインインする必要のない代替のクラウド ストレージ API があれば幸いです。

どんな助けでも大歓迎です!

4

1 に答える 1

0

解決策は、サービス アカウントまたは通常のアカウントを使用してアプリケーション所有のアカウントを使用することです。https://developers.google.com/drive/web/service-accountsの指示に従ってください。

于 2014-09-04T11:37:37.747 に答える