简体   繁体   中英

Using OAuth and gdata to copy google spreadsheet

I am working with 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')

I want to achieve this with OAuth rather than a separate e-mail/password combination for the ClientLogin (or any method that can get the desired results; documentation seems nightmarishly poor for the Google APIs). Is there a simple way to do this?

With the new Drive API, I have found a way and hope it can be helpful for others:

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

The new version 3.0 of the Spreadsheet API is still only available in gdata. Google Drive API does not include the Spreadsheet API.

Docs .

There is a warning on the gdata API home page and it reads:

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.

It does not claim that gdata is completely deprecated. It is misleading to state that gdata is deprecated.

gdata is deprecated. The new (2012) Drive API is the way to do it. It's all documented at https://developers.google.com/drive/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM