Python 创建包含文本内容的新文件google drive api?
我正在尝试使用GoogleDrive API创建一个python函数,以创建一个包含内容的新GoogleDrive文件 我有点困惑(在查看文档之后),应该使用哪个URI,以及创建这个文件需要在响应体中放置什么。到目前为止,这是创建新文件的函数,也是进行API调用的函数(正在运行): 目前,这不会创建文件,也没有文件的任何内容。我可以做些什么来修复我当前拥有的内容,并将内容添加到文件中Python 创建包含文本内容的新文件google drive api?,python,file,google-drive-api,Python,File,Google Drive Api,我正在尝试使用GoogleDrive API创建一个python函数,以创建一个包含内容的新GoogleDrive文件 我有点困惑(在查看文档之后),应该使用哪个URI,以及创建这个文件需要在响应体中放置什么。到目前为止,这是创建新文件的函数,也是进行API调用的函数(正在运行): 目前,这不会创建文件,也没有文件的任何内容。我可以做些什么来修复我当前拥有的内容,并将内容添加到文件中 谢谢 我相信你的目标如下 您希望通过使用驱动器API转换为Google文档,将文本数据上载到Google驱动器
谢谢 我相信你的目标如下
- 您希望通过使用驱动器API转换为Google文档,将文本数据上载到Google驱动器。
- 从您的问题和评论中,我了解到脚本中的
是文本数据内容
- 从您的问题和评论中,我了解到脚本中的
- 您的访问令牌可用于使用驱动器API上载文件
- 您希望通过python使用
来实现您的目标请求
- 当我测试上述脚本时,我可以确认新的Google文档(包括
的文本)已粘贴到Google Drive中示例文本1
- 这是一个简单的示例脚本,用于将文本值作为Google文档上传到Google Drive。因此,请根据您的实际情况进行修改
- 您希望通过使用驱动器API转换为Google文档,将文本数据上载到Google驱动器。
- 从您的问题和评论中,我了解到脚本中的
是文本数据内容
- 从您的问题和评论中,我了解到脚本中的
- 您的访问令牌可用于使用驱动器API上载文件
- 您希望通过python使用
来实现您的目标请求
- 当我测试上述脚本时,我可以确认新的Google文档(包括
的文本)已粘贴到Google Drive中示例文本1
- 这是一个简单的示例脚本,用于将文本值作为Google文档上传到Google Drive。因此,请根据您的实际情况进行修改
- 我相信你的目标如下
make\u request()
和gd\u create\u text\u file()中使用的参数正确吗?在上面的代码中,我没有看到任何应该创建文件的行。你不想在哪里创建什么类型的文件?@cheshire我要从Google Doc API文档中删除,在Google docs中创建一个文件。您的问题还不清楚,但正如我所说的,在上面的代码中您没有写入任何文件。下面是一个如何使用python编写.txt文件的小示例,如果您想编写一个二进制文件,请看这个,而不是python中的.txt文件,我正在尝试创建一个包含文本的google docs文件。我正在使用Google Docs API来完成这项工作,因此不应该生成一个常规的.txt文件。谢谢您的回复。从您的回复中,我提出了一个简单的示例脚本来实现您的目标。你能确认一下吗?如果这不是您期望的方向,我很抱歉。您确定在make\u request()
和gd\u create\u text\u file()中使用的参数正确吗?在上面的代码中,我没有看到任何应该创建文件的行。你不想在哪里创建什么类型的文件?@cheshire我要从Google Doc API文档中删除,在Google docs中创建一个文件。您的问题还不清楚,但正如我所说的,在上面的代码中您没有写入任何文件。下面是一个如何使用python编写.txt文件的小示例,如果您想编写一个二进制文件,请看这个,而不是python中的.txt文件,我正在尝试创建一个包含文本的google docs文件。我正在使用Google Docs API来完成这项工作,因此不应该生成一个常规的.txt文件。谢谢您的回复。从您的回复中,我提出了一个简单的示例脚本来实现您的目标。你能确认一下吗?如果这不是你期望的方向,我道歉。
def make_request(self, method, url, url_params=dict(),
headers=dict(), body=None):
# add Authorization header
headers["Authorization"] = "Bearer " + self.token # bearer authentication
# make the request
try:
r = requests.request(method, url, headers=headers, params=url_params, data=body)
except Exception as e:
return 9999, str(e)
# get the body of the response as text
body = r.text
# return value contains the error message or the body
if r.status_code > 299:
res_dict = json.loads(body)
ret_val = res_dict["error"]["message"]
else:
ret_val = body
return r.status_code, ret_val
def gd_create_text_file(self, name, parent_id, contents):
request_body = {
"name": name,
"parents": "[" + parent_id + "]",
"mimeType": "application/vnd.google-apps.document"
}
request_body_json = json.dumps(request_body)
header = {
"Content-Type": "application/json"
}
create_json = self.make_request("POST", "https://www.googleapis.com/upload/drive/v3/files/", headers=header, body=request_body_json)
if(create_json[0] != 200):
# an error code was thrown, return None
print("Error" + str(create_json[0]))
return None
else:
#no error, create and return dictionary
dictionary = {
"id": json.loads(create_json[1])["id"],
"name": json.loads(create_json[1])["name"]
}
print("dict: " + str(dictionary))
return dictionary
import io
import json
import requests
token = '###' # Please set your access token.
name = 'sample' # Please set the filename on Google Drive.
parent_id = 'root' # Please set the folder ID. If you use 'root', the file is created to the root folder.
contents = 'sample text 1' # This is a sample text value for including the created Google Document.
para = {
"name": name,
"parents": [parent_id],
"mimeType": "application/vnd.google-apps.document"
}
res = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers={"Authorization": "Bearer " + token},
files={
'metadata': ('metadata', json.dumps(para), 'application/json'),
'file': ('file', io.BytesIO(contents.encode('utf-8')), 'text/plain')
}
)
print(res.text)