我创建了服务帐户凭据,在AWS实例上运行我的代码。 代码是:
from google.oauth2 import service_account
SCOPES = [ 'https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/drive' ]
SERVICE_ACCOUNT_FILE = 'serviceAccount.json'
APPLICATION_NAME = 'app name'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE, scopes=SCOPES)
drive_service = discovery.build('drive', 'v2', credentials=credentials)
settings = configparser.ConfigParser()
settings._interpolation = configparser.ExtendedInterpolation()
settings.read('caremarkets.ini')
origin_file_id = settings.get('GoogleSheetParams',''+manualType+'Manual2018')
folderId = settings.get('GoogleSheetParams',''+manualType+'FolderID')
timeStamp = '{:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now())
title = manualType+ " Userid: ( "+str(user_id)+" ) "+timeStamp
fileObject= drive_service.files().copy(
fileId=origin_file_id, body={"parents": [{"kind": "drive#fileLink",
"id": folderId}], 'title': title}).execute()
然而这是我的错误:第41行是服务调用,如上面的代码片段所示:
fileObject= drive_service.files().copy(
fileId=origin_file_id, body={"parents": [{"kind": "drive#fileLink",
"id": folderId}], 'title': title}).execute()
File "/home/ubuntu/dev_deploy/copy_master_spreadsheet_to_user_slave.py", line 41, in copy_master_spreadsheet_to_user_slave
"id": folderId}], 'title': title}).execute()
File "/usr/local/lib/python3.5/dist-packages/oauth2client/_helpers.py", line 133, in positional_wrapper
return wrapped(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/googleapiclient/http.py", line 844, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v2/files/1lNcqvQACH6DOBl3uQywJO1A6Iy6va7YdSrZiobMaOj0/copy?alt=json returned "File not found: 1lNcqvQACH6DOBl3uQywJO1A6Iy6va7YdSrZiobMaOj0">
请注意,上面的代码在我的机器上可以正常工作。我改变代码后使用服务帐户而不是通常的client_secret.json
服务帐户密钥信息:
Service account name Service account ID Key ID Key creation date Options
SACxxxxxxs sacxxxxxxs@drivesheets-197604.iam.gserviceaccount.com bf287f0baa29d4fa5f6b98aacf03b8bff13b0447 Mar 15, 2018
非常感谢你能提供帮助
答案 0 :(得分:1)
找不到文件意味着您所提供的文件ID不存在,或者您通过身份验证的用户无法访问它。
获取服务帐户电子邮件地址并转到Google驱动器的网络版,与服务帐户共享该文件。然后它就可以访问相关文件了。