我刚开始使用googledriveapi,在理解如何使用该API时遇到了很多困难。在搜索了整个网络之后,我终于找到了一个工作示例here,并获得了以下代码:
from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build
from googleapiclient.http import MediaFileUpload
#Set up a credentials object I think
creds = ServiceAccountCredentials.from_json_keyfile_name('client_secrets.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.csv', mimetype = 'text/csv')
#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')))
当我运行上述代码时,我得到以下结果:
Uploading file test...
Created file 'test' id '12kV2WUPg64_asVgVaVFF0X3lFAA_RkVJGtL-eDnexxM'.
这意味着代码执行成功,所以我希望看到测试.csv在我的谷歌硬盘上的某个地方,但事实并非如此,而且该文件在哪里都找不到。所以,有几个问题:
任何帮助都是非常感谢的,因为几乎不可能在一个地方找到这个googleapi的所有工作部件。你知道吗
您使用的是服务帐户,因此文件是在属于该帐户的驱动器中创建的,而不是在您的个人帐户中创建的。你知道吗
相关问题 更多 >
编程相关推荐