gpt4 book ai didi

python - 如何使用 REST API 复制 protected Google 表格范围内的用户?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:26:18 26 4
gpt4 key购买 nike

我正在尝试使用 HTTP get 请求复制 Google 表格。以下是我的代码的摘录。

    data = {"title": name_gdocs, "description": record_url, "parents": fields_dict['parents']}
request_url = "https://www.googleapis.com/drive/v2/files/%s/copy?access_token=%s" % (template_id, access_token)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
data_json = json.dumps(data)

req = urllib2.Request(request_url, data_json, headers)
content = urllib2.urlopen(req, timeout=TIMEOUT).read()
content = json.loads(content)

工作表被复制,工作表中的 protected 范围也被复制,但复制文档的 protected 范围只能由所有者访问,与原始文档的指定用户不同。

我可以验证有权访问原始文档的用户是否仍然可以访问新文档。他们只是失去了对 protected 范围的访问权限。

是否有某种标志可以用来制作深拷贝或类似的东西?

此外,我更愿意仅使用驱动器 API 来执行此操作,因为我无权访问此项目的工作表 API。否则我只会使用它。

最佳答案

解决方案使用 Sheets API。 Drive API 没有复制 protected 范围内所有者的功能。请记住,我使用的是 REST API,但本质上应该使用特定于语言的库进行相同的函数调用。

复制

documentation 中指定的 url 语法制作副本是 POST https://www.googleapis.com/drive/v2/files/{fileId}/copy

可以这样调用:

data = {"title": name_of_doc, "description": short_description, "parents": list_of_parent_folders}
# On absence of parents folder,the copy will be places in the root directory of the drive.

data_json = json.dumps(data)
request_url = "https://www.googleapis.com/drive/v2/files/%s/copy?access_token=%s" % (template_id, access_token)
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
req = urllib2.Request(request_url, data_json, headers)
content_json = urllib2.urlopen(req, timeout=TIMEOUT).read()
content = json.loads(content_json)

获取 protected 范围的所有者

documentation表示请求需要使用GET方法如下GET https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}

注意:update_protected_range_to_google_sheet 函数将在稍后定义

if content.get('id') and content.get('mimeType','') == 'application/vnd.google-apps.spreadsheet':
# Make sure that the document is a Google Sheet

request_url = "https://sheets.googleapis.com/v4/spreadsheets/%s?access_token=%s" % (template_id, access_token)
s_req = urllib2.Request(request_url, None, headers)
s_content = urllib2.urlopen(s_req, timeout=TIMEOUT).read()
s_content = json.loads(s_content)
for sheet in s_content.get('sheets', []):
for pr in sheet.get('protectedRanges', []):
update_protected_range_to_google_sheet(access_token, content.get('id'), pr)

应用所有者

documentation顾名思义,使用对 Sheets API 的批量更新调用,可以一次完成许多请求,但在这种情况下,出于说明目的,每次更新都会调用一次该函数。这是我使用的将被调用的函数:

def update_protected_range_to_google_sheet(self,access_token, sheet_id,range_info):
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
request_url = "https://sheets.googleapis.com/v4/spreadsheets/%s:batchUpdate?access_token=%s" % (sheet_id, access_token)
data = {
"requests": [
{
"updateProtectedRange": {
"protectedRange": range_info,
"fields": "namedRangeId,warningOnly,editors"
}
}
]
}
data_json = json.dumps(data)
req = urllib2.Request(request_url, data_json, headers)
content_json = urllib2.urlopen(req, timeout=TIMEOUT).read()
content = json.loads(content_json)

结论

我无法使用 Sheets API 的主要原因是我使用的系统有一个围绕 Drive API 的自定义构建包装器,并且身份验证被硬编码为指向库创建者拥有的项目。

这不行,所以我在 console 上创建了自己的项目并从中生成一个 token 。新项目启用了 Drive API 以及 Sheets API - 原始项目显然没有。

关于python - 如何使用 REST API 复制 protected Google 表格范围内的用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39163323/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com