24

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
4

2 に答える 2

34

BytesIO から FileIO に変更すると、ファイルを実際にダウンロードできるようになりました。これは、コードを次のように変更した行です。

fh = io.FileIO(filename, 'wb')

ファイルをダウンロードできる完全なコードは次のとおりです。

def download_file(file_id, mimeType, filename):
    if "google-apps" in mimeType:
        # skip google files
        return
    request = drive_service.files().get_media(fileId=file_id)
    fh = io.FileIO(filename, 'wb')
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print "Download %d%%." % int(status.progress() * 100)


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)

    print 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)
    files = drive_service.files().list().execute()
    for f in files['files']:
        print f['name']
        download_file(f['id'], f['mimeType'], f['name'])
于 2016-03-26T17:23:10.353 に答える
12

ファイルはダウンロード中ですが、Google が提供する例ではファイルは何も処理されません。

このように BytesIO バッファの内容を返すだけです (最後に return を追加するだけです)...

def download_file(service, file_id):
    request = service.files().get_media(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))
    return fh.getvalue()
于 2016-03-23T17:07:40.247 に答える