oauth と API を使用して Google ドライブからファイルをダウンロードする方法をいくつか試しましたが、ファイルをダウンロードできません。私は適切に認証されたと信じています。コードを実行した後、ファイルのダウンロードは成功したようですが (エラーはありません)、ファイルはダウンロードされませんでした。
これは私がこれまでに試したコードです:
def download_file(file_id, mimeType):
if "google-apps" in mimeType:
return
request = drive_service.files().get(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print "Download %d%%." % int(status.progress() * 100)
ただし、これは「ダウンロード 100%」になります。コンソールに出力されますが、ファイルはダウンロードされません。
私も試しました:
def download2(download_url):
resp, content = drive_service._http.request(download_url)
if resp.status == 200:
print 'Status: %s' % resp
return content
else:
print 'An error occurred: %s' % resp
return None
これもダウンロード ファイルを生成しませんが、200 メッセージが表示されます。
これらはどちらも、API と適切に接続しているように見えます。実際にコンピューターにファイルを取得するために必要な追加の手順はありますか?
編集:
これは私のコードの残りの部分でした:
import json
import webbrowser
import httplib2
import io
from apiclient.http import MediaIoBaseDownload
from apiclient import discovery
from oauth2client import client
if __name__ == '__main__':
flow = client.flow_from_clientsecrets(
'client_secrets.json',
scope='https://www.googleapis.com/auth/drive.readonly',
redirect_uri='urn:ietf:wg:oauth:2.0:oob')
auth_uri = flow.step1_get_authorize_url()
webbrowser.open(auth_uri)
auth_code = raw_input('Enter the auth code: ')
credentials = flow.step2_exchange(auth_code)
http_auth = credentials.authorize(httplib2.Http())
drive_service = discovery.build('drive', 'v3', http_auth) #also tried v2
files = drive_service.files().list().execute()
for f in files['files']:
#call one of the two download methods with the proper arguments