Python带有Google Drive API [英] Python w/Google Drive API

查看:788
本文介绍了Python带有Google Drive API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人熟悉Google Drive API,告诉我我的代码在做什么错?我只是试图使用API​​和服务帐户将文件传输到我的谷歌驱动器。当我运行它时,我得到一个文件ID,但该文件实际上并没有显示在我的驱动器中,所以我不知道该文件实际上在哪里...



<$从oauth2client.service_account导入ServiceAccountCredentials
从apiclient.discovery导入构建$ b $从apiclient.http导入MediaFileUpload

#设置凭证对象我认为
creds = ServiceAccountCredentials.from_json_keyfile_name('service_account.json',['https://www.googleapis.com/auth/drive'])

#Now构建我们的api对象,事物
drive_api = build('drive','v3',credentials = creds)

file_name =test.html
print正在上传文件+ file_name + ...

#我们必须提出一个请求散列函数来告诉google API我们给出的内容
body = {'name':file_name,'mimeType':'application /vnd.google-apps.document'}

#现在创建媒体文件上传对象,并告诉它要上传什么文件,
#在这种情况下'test.html '
media = MediaFileUpload('test.html',mimetype ='text / html')

#现在我们正在做实际的文章,创建一个新文件的上传类型
fiahl = drive_api.files()。create(body = body,media_body = media).execute()

#因为详细程度很好
printCreated file'%s' ID'%s'。 fiahl.get('id')


解决方案

在您的示例脚本中,我认为Python Quickstart很容易理解,因此它使用Python快速入门( https://developers.google.com/drive/v3/web/quickstart/python )。


  1. 请在Python Quickstart中执行第1步和第2步。

  2. Python快速入门如下。

来自

<1>


  SCOPES ='https://www.googleapis.com/auth/drive.metadata .readonly'



<2>


  results = service.files()。list(
pageSize = 10,fields =nextPageToken,files(id,name))。execute()
items = results .get('files',[])
如果不是项目:
print ('找不到文件')
else:
print('Files:')
用于物品中的物品:
print('{0}({1})' .format(item ['name'],item ['id']))

收件人:



<1>


  SCOPES =' https://www.googleapis.com/auth/drive.file'

2。

  f ='test.html'
mime ='text / html'
body = {
'name':f,
'mimeType':mime
''parents':## folderID ##,#如果你想上传文件夹下的文件,请使用它。
}
results = service.files()。create(
body = body,
media_body = MediaFileUpload(f,mimetype = mime,resumable = True)
) .execute()
print(results)

当文件上传时,遵循JSON可以得到。您可以在Google云端硬盘中看到该文件。

  {
mimeType:text / html,
name:test.html,
id:#####,
kind:drive#file
}


Is anyone familiar enough with the Google Drive API to tell me what I am doing wrong with my code below? I am just trying to transfer a file to my google drive using the API and a service account. When I run it, I get a file ID, but the file doesnt actually show up in my drive, so I have no idea where the file is actually going...

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('service_account.json', ['https://www.googleapis.com/auth/drive'])

#Now build our api object, thing
drive_api = build('drive', 'v3', credentials=creds)

file_name = "test.html"
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')    

解决方案

From your sample script, I thought that Python Quickstart is easy for you to understand. So it uses Python Quickstart at (https://developers.google.com/drive/v3/web/quickstart/python).

  1. Please carry out Step 1 and Step 2 at Python Quickstart.

  2. Please modify the sample script at Python Quickstart as follows.

From :

1.

SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'

2.

results = service.files().list(
    pageSize=10,fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
    print('No files found.')
else:
    print('Files:')
    for item in items:
        print('{0} ({1})'.format(item['name'], item['id']))

To :

1.

SCOPES = 'https://www.googleapis.com/auth/drive.file'

2.

f = 'test.html'
mime = 'text/html'
body = {
    'name': f,
    'mimeType': mime
    # 'parents': ## folderID ##, # If you want to upload file under folder, please use this.
}
results = service.files().create(
    body=body,
    media_body=MediaFileUpload(f, mimetype=mime, resumable=True)
).execute()
print(results)

When the file was uploaded, following JSON can be obtained. And you can see the file on Google Drive.

{
  "mimeType": "text/html",
  "name": "test.html",
  "id": "#####",
  "kind": "drive#file"
}

这篇关于Python带有Google Drive API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆