3

thisthisを組み合わせることで、これまでに得たものは次のとおりです。

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";

//First, build a Drive service object authorized with the service accounts
$auth = new Google_AssertionCredentials(
    DRIVE_SERVICE_ACCOUNT_EMAIL,
    array( DRIVE_SCOPE ),
    file_get_contents( DRIVE_SERVICE_ACCOUNT_KEY )
);
$client = new Google_Client();
$client->setUseObjects( true );
$client->setAssertionCredentials( $auth );
$service = new Google_DriveService( $client );

//Then, insert the file
$file = new Google_DriveFile();
$file->setTitle( 'My document' );
$file->setMimeType( 'text/plain' );
$createdFile = $service->files->insert( $file, array(
    'data' => 'Hello world!',
    'mimeType' => 'text/plain',
));

print_r( $createdFile );

つまり、テキスト/プレーン ファイルを作成し、そのメタデータを含む配列を返します。ただし、テキスト/プレーンではなく、Googleドキュメントを作成したいのです。もちろん、MIME タイプ (両方の外観) を「application/vnd.google-apps.document」に変更しようとしましたが、次のようになりました。

致命的なエラー: キャッチされない例外 'Google_ServiceException' とメッセージ 'Error calling POST https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart : (400) Bad Request' in /local/path/to/ google-api-php-client/src/io/Google_REST.php:66

また、ドキュメントを公開する必要があります。上記の方法で text/plain ファイルをアップロードし、(アップローダーとは別のアカウントから) そのファイルを参照しようとすると、許可が必要だと言われます。の配列を詳しく調べたところ、$createdFile値のない「共有」キーに気付いたので、非常に単純に次のように 1 に設定してみました。

$file->setShared( 1 );

それは機能しませんでした。また、配列をもう一度見てみると、「共有」キーにはまだ値が割り当てられていないことに気付きました。私に役立つ可能性のあるドキュメントをオンラインで探しましたが、運がありませんでした。誰でも私を助けることができますか?ありがとう!!

4

2 に答える 2

9

多くの読書とテストの後、私は自分の質問に対する答えを見つけました。

問題は、ファイルの 'data' パラメータでした。Google ドキュメントはプレーン テキストをデータとして持つことができません (データにはヘッダーなどを含める必要があります)。したがって、「data」パラメータ(および「mimeType」も削除する必要があります)を削除すると、エラーが消えました。

アクセス許可に関しては、解決策は、最初にデフォルトのアクセス許可でファイルを作成し、次に新しいアクセス許可を追加することでした。

以下に、一般公開されている Google ドキュメントを作成するための最小限のコードを貼り付けます。

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";

//Build the Drive Service object authorized with your Service Account
$auth = new Google_AssertionCredentials(
    DRIVE_SERVICE_ACCOUNT_EMAIL,
    array( DRIVE_SCOPE ),
    file_get_contents( DRIVE_SERVICE_ACCOUNT_KEY )
);
$client = new Google_Client();
$client->setUseObjects( true );
$client->setAssertionCredentials( $auth );
$service = new Google_DriveService( $client );

//Create the file
$file = new Google_DriveFile();
$file->setTitle( 'Hello world!' );
$file->setMimeType( 'application/vnd.google-apps.document' );
$file = $service->files->insert( $file );

//Give everyone permission to read and write the file
$permission = new Google_Permission();
$permission->setRole( 'writer' );
$permission->setType( 'anyone' );
$permission->setValue( 'me' );
$service->permissions->insert( $file->getId(), $permission );

print_r( $file );
于 2013-08-19T10:15:39.957 に答える
1

私はこれを Moodle LMS システムのプラグインとして実装しました。これがソース コードです。これには、ドキュメントへの適切な権限の作成と設定を実装する PHP クラスが含まれています。 https://github.com/nadavkav/moodle-mod_googledrive

実際の関連クラスはこちら: https://github.com/nadavkav/moodle-mod_googledrive/blob/master/classes/googledrive.php

于 2016-11-14T10:17:46.273 に答える