API请求错误:HTTPError 400,“错误请求”

时间:2019-08-10 18:14:25

标签: machine-learning python-3.7 pydrive

我正在尝试使用PyDrive将文件上传到Pycharm中的Google云端硬盘,但仍然出现上述错误。我已经看过许多关于SO的其他问题,但找不到答案。下面是完整的错误消息。

Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\pydrive\files.py", line 369, in _FilesInsert
    http=self.http)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\googleapiclient\http.py", line 826, in execute
    _, body = self.next_chunk(http=http, num_retries=num_retries)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\googleapiclient\http.py", line 994, in next_chunk
    return self._process_response(resp, content)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\googleapiclient\http.py", line 1025, in _process_response
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v2/files?alt=json&uploadType=resumable returned "Bad Request">

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/ab123/PycharmProjects/tensorflow_test/LiveAnalyzer2.py", line 194, in <module>
    upload(drive1)
  File "C:/Users/ab123/PycharmProjects/tensorflow_test/LiveAnalyzer2.py", line 186, in upload
    file_drive.Upload()
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\pydrive\files.py", line 285, in Upload
    self._FilesInsert(param=param)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\pydrive\auth.py", line 75, in _decorated
    return decoratee(self, *args, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\tensorflow_test\lib\site-packages\pydrive\files.py", line 371, in _FilesInsert
    raise ApiRequestError(error)
pydrive.files.ApiRequestError: <HttpError 400 when requesting https://www.googleapis.com/upload/drive/v2/files?alt=json&uploadType=resumable returned "Bad Request">

对于上下文,我正在程序中运行两个机器学习模型,如果发生某种预测,该文件将上传到云端硬盘。

这是我的上传功能:

def upload(drive):
    with open("C:/Users/ab123/Desktop/AlertLog.txt", "r") as file:
        file_drive = drive.CreateFile({'title': os.path.basename(file.name)})
        file_drive.SetContentString(file.read())
        file_drive.Upload()

错误跟踪指向最后一行,但是我无法弄清楚问题出在哪里。

我希望代码能够正常运行,并在我的GDrive中显示一个名为“ AlertLog.txt”的文件。

0 个答案:

没有答案