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