VB.NET で Google Data API を使用して、テキストを Googleドキュメント に保存したり、Streamに読み込むサンプルコードが見つからなかったので記録。
まずは、テキストを Googleドキュメント に保存。
'この変数にエントリーの内容を設定 Dim txtData As String = "これはサンプルテキストの内容です。" 'アップロード前に同名のエントリーを取得 Dim ds As New Google.GData.Documents.DocumentsService("applicationName") ds.setUserCredentials("username", "password") Dim dlq As New Google.GData.Documents.DocumentsListQuery dlq.Title = "サンプルテキスト" Dim df As Google.GData.Documents.DocumentsFeed = ds.Query(dlq) 'アップロードするエントリーの内容をファイルに出力してからアップロード Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\sample.txt", False) sw.Write(txtData) sw.Close() Dim denew As Google.GData.Documents.DocumentEntry denew = ds.UploadDocument("C:\sample.txt", "サンプルテキスト") 'アップロード前に取得したエントリーを削除 For Each de As Google.GData.Documents.DocumentEntry In df.Entries de.Delete() Next
次に、Googleドキュメント からテキストを読み込み。
'この変数にエントリーの内容を取得 Dim txtData As String 'エントリーを取得し、テキストを読み込む Dim gc As New Google.GData.Client.GDataCredentials("username", "password") Dim rs As New Google.GData.Client.RequestSettings("applicationName", gc) Dim dr As New Google.Documents.DocumentsRequest(rs) Dim f As Google.GData.Client.Feed(Of Google.Documents.Document) f = dr.GetEverything() For Each d As Google.Documents.Document In f.Entries If d.DocumentEntry.Title.Text = "サンプルテキスト" Then 'エントリーの内容をストリームにダウンロードしてテキストを読み込む Dim st As System.IO.Stream = dr.Download(d, Google.Documents.Document.DownloadType.txt) Dim sr As New System.IO.StreamReader(st) txtData = sr.ReadToEnd sr.Close() st.Close() Exit For End If Next
Google Data API テキストの保存と読み込み