2016-03-23 10 views
0

これはGoogleドライブv3 APIのコードのコードです。ドライブAPIを使用してファイルの説明を印刷するにはどうすればよいですか?

デフメイン(): "" "GoogleドライブAPIの基本的な使用方法を表示します" "" 資格情報= get_credentials() のhttp = credentials.authorize(httplib2.Http()) サービス= discovery.build ( 'ドライブ'、 'v3'、http = http)

results = service.files().list(q="mimeType='image/jpeg'", 
    pageSize=2,fields="nextPageToken, files(id, mimeType, name, description)").execute() 
items = results.get('files', []) 
if not items: 
    print('No files found.') 
else: 
    print('Files:') 
    for item in items: 
     print('{0} {1} {2} {3}'.format(item['id'], item['mimeType'], item['name'], item['description'])) 

v3のドキュメントの有効な属性のように見える説明を追加しました。私はこのエラーが発生しています。

print('{0} {1} {2} {3}'.format(item['id'], item['mimeType'], item['name'], item['description'])) 

KeyError例外: '説明'

私は、Python 3.0を使用しています。元の例のコードはここで見つけることができます - https://developers.google.com/drive/v3/web/quickstart/python#step_3_set_up_the_sample

ファイルリファレンスのドキュメントはここで見つけることができます - 説明が有効であるものの https://developers.google.com/drive/v3/reference/files

答えて

1

、いないすべてのファイルが記述を持っているし、彼らドン場合しますdescription属性は返されません。試してみてください:

for item in items: 
    description = item.get('description', '') 
    print('{0} {1} {2} {3}'.format(item['id'], item['mimeType'], item['name'], description)) 

これは属性が項目に設定されていない場合、説明が設定または「」(空白)されている場合、APIによって返された属性に説明を設定します。

+0

私を始めてくれてありがとう!ドライブAPIがデフォルトで空の文字列を返さないのはなぜかと思います。 – abhi

関連する問題