Google drive API下载文件-python-未下载任何文件
我尝试了很多方法通过oauth和API从google drive下载文件,但是我无法下载文件。我相信我已经正确地证明了。运行我的代码后,似乎下载文件成功(没有错误),但没有下载任何文件 这是我迄今为止尝试过的代码:Google drive API下载文件-python-未下载任何文件,python,google-drive-api,oauth2,Python,Google Drive Api,Oauth2,我尝试了很多方法通过oauth和API从google drive下载文件,但是我无法下载文件。我相信我已经正确地证明了。运行我的代码后,似乎下载文件成功(没有错误),但没有下载任何文件 这是我迄今为止尝试过的代码: def download_file(file_id, mimeType): if "google-apps" in mimeType: return request = drive_service.files().get(fileId=file_id)
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
该文件正在下载,但谷歌给出的示例对该文件没有任何作用 您只需要像这样返回BytesIO缓冲区的内容(只需在末尾添加一个返回)
从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'])
hrmm,所以我添加了open(filename,'wb')作为输出:fh.seek(0)output.write(fh.read())到download_file函数的末尾。但它只是下载文件的元数据。@user1253952
html=download_file(service,'file_id'),open('file.csv','wb')为f:f.write(html)
谢谢,因为某种原因fh.read()
不起作用,但fh.getvalue()
起作用。它是一个缓冲区。检查这个ref:No。发生的是auth_uri将是一个生成的url,您需要发送给驱动器帐户的所有者。当他们访问该链接时,他们将不得不批准你,然后会得到一个代码,他们将不得不返回给你。然后输入该代码。请注意,该代码只在有限的时间内有效一次(可能是15分钟)。是的,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'])