借助新的 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