【问题标题】:Server-side Google API service request yields googleapiclient.errors.HttpError: <HttpError 404服务器端 Google API 服务请求产生 googleapiclient.errors.HttpError: <HttpError 404
【发布时间】:2018-08-26 21:48:59
【问题描述】:

我创建了服务账户凭证,在 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

如果你能帮忙,非常感谢

【问题讨论】:

    标签: oauth google-api google-drive-api google-sheets-api service-accounts


    【解决方案1】:

    file not found 意味着您提供的文件 ID 不存在或您通过身份验证的用户无权访问它。

    获取服务帐户电子邮件地址并转到网络版 Google 云端硬盘与服务帐户共享文件。然后它将可以访问相关文件。

    【讨论】:

    • DalmTo 对此的回答令我满意。要为答案添加更多信息: 1.登录到我的电子邮件地址,该地址源自 Google Drive 上的文件 2.导航到包含具有 http 404 的文件的文件夹 3.右键单击该文件夹并选择“共享...” 4. 在与其他人共享对话框中,为我的“服务帐户名称”输入“服务帐户 ID”(即电子邮件地址),并将角色设置为“可以组织、添加和编辑”(这就是我需要)
    • @DalmTo 和 ElliotB,我尝试了您的解决方案。似乎确实解决了这个问题。有什么想法吗?
    • 就我而言,我无权访问应该存储文件的文件夹。当问题是权限问题而不是文件存在问题时,我发现作为最终用户出现“FileNotFound”错误而不是“错误权限”错误是很可怕的。..
    • @vinzee 告诉黑客你没有权限然后告诉他们文件确实存在。我个人认为最好说没有找到
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    • 1970-01-01
    • 2016-02-27
    • 2019-01-06
    • 2018-12-06
    相关资源
    最近更新 更多