标签:python google-drive-sdk google-sheets gdata gspread
我正在使用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,google-drive-sdk,google-sheets,gdata,gspread 来源: https://codeday.me/bug/20190703/1361669.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。