gpt4 book ai didi

python - 使用 OAuth 和 gdata 复制谷歌电子表格

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:40 25 4
gpt4 key购买 nike

我正在使用 gspread修改现有的 Google 电子表格并想复制现有的电子表格。不幸的是,gspread 不支持这一点,但可以使用 gdata 来完成(如 this thread 中所述):

import gdata.docs.client

docs_client = gdata.docs.client.DocsClient()
docs_client.ClientLogin('ashe@pokemon.com', 'Pikachu', 'Any non empty string')
base_resource = docs_client.GetResourceById(resource_id)
new_resource = docs_client.copy_resource(base_resource, 'pokedex')

我想通过 OAuth 来实现这一点,而不是为 ClientLogin 使用单独的电子邮件/密码组合(或任何可以获得所需结果的方法;Google API 的文档似乎非常糟糕)。有没有简单的方法可以做到这一点?

最佳答案

通过新的 Drive API,我找到了一种方法,希望它能对其他人有所帮助:

import httplib2
from apiclient import errors
from apiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials

to_copy = '<id/key_string_from_desired_file_url>'
# Service account e-mail from Google dev console
drive_id = '<my_long_service_account_string>@developer.gserviceaccount.com'
# Get the right permissions
drive_scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
# pem key converted from p12 key generated in dev console
with open(os.path.abspath('my_key.pem'), 'rb') as keyfile:
drive_key = keyfile.read()

credentials = SignedJwtAssertionCredentials(drive_id, drive_key, drive_scope)

http = httplib2.Http()
http = self.credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
file_copy = {'title': title}

try:
drive_service.files().copy(fileId=to_copy, body = file_copy).execute()
except errors.HttpError, error:
print error

关于python - 使用 OAuth 和 gdata 复制谷歌电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28102260/

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