Google Drive API v3在python

时间:2017-05-16 01:41:55

标签: python google-drive-api google-api-client

我正在尝试使用一些新数据更新我创建的文件。本质上,我试图将内容附加到已创建的文件。我尝试过使用不同属性的不同方法,但到目前为止似乎没有任何工作。从v2到v3的迁移似乎使python中的开发变得更加困难。

这是我目前的代码

def updateFile(fileID, contents):
    credentials = get_credentials() #this function gets the credentials for oauth
    http = credentials.authorize(httplib2.Http())
    service = discovery.build('drive', 'v3', http=http)
    # First retrieve the file from the API.

    #fileCreated = service.files().get(fileId=fileID).execute()
    #print(dir(fileCreated.update()))
    # File's new content.
    file_metadata = { 'name' : 'notes.txt', 'description' : 'this is a test' }
    fh  = BytesIO(contents.encode())
    media = MediaIoBaseUpload(fh,
                        mimetype='text/plain')
    # Send the request to the API.
    #print(BytesIO(contents.encode()).read())
    print(fileID)
    updated_file = service.files().update(
        body=file_metadata,
        #uploadType = 'media',
        fileId=fileID,
        #fields = fileID,
        media_body=media).execute()

我尝试过使用MediaFileUpload(它仅用于上传文件),但我附加了一个'字符串'这是使用语音界面生成的实时时间,不存储在任何文件中。所以我最终使用了MediaIoBaseUpload。

代码运行时没有任何错误,但文件未更新。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:0)

好的,所以我发现我的代码出了什么问题。显然,我发布的这一大块代码没有任何问题。我意识到我没有将我创建的文档转换为“google文档”,因此代码没有更新文档。我将我创建的文档的mime类型更改为google文档,现在代码工作正常:)

答案 1 :(得分:0)

您还可以使用let items = ["Male", "Female"] let customSC = UISegmentedControl(items: items) customSC.selectedSegmentIndex = 0 let frame = UIScreen.mainScreen().bounds customSC.frame = //Your Frame customSC.tintColor = UIColor.blackColor() customSC.addTarget(self, action: "yourMethod:", forControlEvents: .ValueChanged) self.view.addSubview(customSC) 获取文件内容并向其添加新内容。