Tôi đã tìm thấy câu hỏi này trong khi cố gắng tìm ra nơi mà "MediaFileUpload" xuất phát từ các ví dụ về API của Google và cuối cùng tôi đã tìm ra. Đây là một ví dụ mã hoàn chỉnh hơn mà tôi đã sử dụng để kiểm tra mọi thứ bằng Python 2.7.
Bạn cần tệp thông tin xác thực JSON để mã này hoạt động. Đây là tệp thông tin đăng nhập bạn nhận được từ ứng dụng/dự án/điều Google của bạn.
Bạn cũng cần tệp để tải lên, tôi đang sử dụng "test.html" ở đây trong ví dụ.
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
from apiclient.http import MediaFileUpload
#Set up a credentials object I think
creds = ServiceAccountCredentials.from_json_keyfile_name('credentials_from_google_app.json', ['https://www.googleapis.com/auth/drive'])
#Now build our api object, thing
drive_api = build('drive', 'v3', credentials=creds)
file_name = "test"
print "Uploading file " + file_name + "..."
#We have to make a request hash to tell the google API what we're giving it
body = {'name': file_name, 'mimeType': 'application/vnd.google-apps.document'}
#Now create the media file upload object and tell it what file to upload,
#in this case 'test.html'
media = MediaFileUpload('test.html', mimetype = 'text/html')
#Now we're doing the actual post, creating a new file of the uploaded type
fiahl = drive_api.files().create(body=body, media_body=media).execute()
#Because verbosity is nice
print "Created file '%s' id '%s'." % (fiahl.get('name'), fiahl.get('id'))
Một danh sách các loại Mime hợp lệ để sử dụng trong "cơ thể" băm có sẵn tại https://developers.google.com/drive/v3/web/mime-types
Một danh sách các chuỗi Mimetype hợp lệ cho MediaFileUpload (họ sẽ cố gắng để chuyển đổi tập tin của bạn để bất cứ điều gì bạn đặt ở đây):
https://developers.google.com/drive/v3/web/integrate-open#open_files_using_the_open_with_contextual_menu
chắc chắn, chúng tôi có thể giúp bạn chắc chắn. Bạn đã có gì? Dán nó và chúng ta có thể sửa nó. Bạn đã thấy điều này chưa, bằng cách này: https://code.google.com/p/google-drive-sdk-samples/source/browse/#hg%2Fpython –
Tôi đã kết thúc mã đó, nhưng khi thử để sử dụng nó, không thể tìm thấy bất kỳ ví dụ nào về biểu mẫu bạn muốn đăng lên JSON. Bạn có thể giúp tôi với đó? Biểu mẫu sẽ trông như thế nào? – user1501783
Khám phá trang này: https://developers.google.com/drive/v3/web/folder#inserting_a_file_in_a_folder nó có ví dụ sử dụng và có thể giúp bạn – Greg