使用 OAuth 和 gdata 复制 google 电子表格

Using OAuth and gdata to copy google spreadsheet

我正在与 gspread to modify existing Google spreadsheets and would like to make a copy of an existing one. Unfortunately, gspread doesn't support this, but it can be done with gdata (as described in 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 来实现这一点,而不是单独的 e-mail/password ClientLogin 组合(或任何可以获得所需结果的方法;Google API 的文档似乎非常糟糕)。有没有简单的方法可以做到这一点?

gdata 已弃用。新的 (2012) 驱动器 API 是实现它的方法。全部记录在 https://developers.google.com/drive/

有了新的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

电子表格 API 的新版本 3.0 仍然仅在 gdata 中可用。 Google 驱动器 API 不包括电子表格 API。

Docs.

gdata API home page 上有一个警告,上面写着:

Warning: Most newer Google APIs are not Google Data APIs. The Google Data APIs documentation applies only to the older APIs that are listed in the Google Data APIs directory. For information about a specific new API, see that API's documentation. For information about authorizing requests with a newer API, see Google Accounts Authentication and Authorization.

它并没有声称 gdata 已完全弃用。声明 gdata 已弃用是一种误导。