Python Google 驱动器 API discovery.build 失败,退出代码为 -1073740777 (0xC0000417)
Python Google Drive API discovery.build fails with exit code -1073740777 (0xC0000417)
我正在构建一个 python 应用程序,它将图像上传到 google 驱动器。然而,在工作了一段时间后,我的 google 驱动器上传突然停止工作。每当我尝试初始化服务时,程序都会以代码 -1073740777 (0xC0000417) 退出。
我已经尝试使用开发者控制台(也使用完全不同的 Google 帐户)创建一个新的 client_secret.json 文件并删除驱动器-python-quickstart.json 凭证文件。
我的朋友使用相同的代码没有这个问题,正如我所说,这对我也有用了一段时间,但突然停止工作了。
我是 运行 Windows 10 Pro x64 Python 3.5 32 位。
当 运行 这个示例程序(取自 the Google quickstart guide)时出现问题:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
"""Shows basic usage of the Google Drive API.
Creates a Google Drive API service object and outputs the names and IDs
for up to 10 files.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
results = service.files().list(maxResults=10).execute()
items = results.get('items', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['title'], item['id']))
if __name__ == '__main__':
main()
在使用 Google Youtube API 时遇到同样的问题,也有相同的 os (Win 10 Pro x64) 并且使用相同的版本哦 python (3.5)。
我不知道如何在主函数中添加这一行帮助
sys.modules['win32file'] = None
好的,我终于自己解决了这个问题。
经过一些调试,我发现了以下内容:
oauth2client 可以使用两种不同的方法打开文件。它首先尝试导入 class _Win32Opener,当导入失败时,它使用 _FcntlOpener。 _Win32Opener 的导入不会失败,但使用 _Win32Opener 打开和锁定文件失败,因此程序崩溃。要强制 oauth2client 使用 _FcntlOpener,只需 remove/rename pyhton 包中的文件 _win32_opener.py
。
相关文件:
[PythonDir]/Lib/site-packages/oauth2client/contrib/locked_file.py
[PythonDir]/Lib/site-packages/oauth2client/contrib/_win32_opener.py
tl;dr
只是remove/rename文件
[PythonDir]/Lib/site-packages/oauth2client/contrib/_win32_opener.py
我正在构建一个 python 应用程序,它将图像上传到 google 驱动器。然而,在工作了一段时间后,我的 google 驱动器上传突然停止工作。每当我尝试初始化服务时,程序都会以代码 -1073740777 (0xC0000417) 退出。
我已经尝试使用开发者控制台(也使用完全不同的 Google 帐户)创建一个新的 client_secret.json 文件并删除驱动器-python-quickstart.json 凭证文件。
我的朋友使用相同的代码没有这个问题,正如我所说,这对我也有用了一段时间,但突然停止工作了。
我是 运行 Windows 10 Pro x64 Python 3.5 32 位。
当 运行 这个示例程序(取自 the Google quickstart guide)时出现问题:
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def main():
"""Shows basic usage of the Google Drive API.
Creates a Google Drive API service object and outputs the names and IDs
for up to 10 files.
"""
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
results = service.files().list(maxResults=10).execute()
items = results.get('items', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['title'], item['id']))
if __name__ == '__main__':
main()
在使用 Google Youtube API 时遇到同样的问题,也有相同的 os (Win 10 Pro x64) 并且使用相同的版本哦 python (3.5)。
我不知道如何在主函数中添加这一行帮助
sys.modules['win32file'] = None
好的,我终于自己解决了这个问题。
经过一些调试,我发现了以下内容:
oauth2client 可以使用两种不同的方法打开文件。它首先尝试导入 class _Win32Opener,当导入失败时,它使用 _FcntlOpener。 _Win32Opener 的导入不会失败,但使用 _Win32Opener 打开和锁定文件失败,因此程序崩溃。要强制 oauth2client 使用 _FcntlOpener,只需 remove/rename pyhton 包中的文件 _win32_opener.py
。
相关文件:
[PythonDir]/Lib/site-packages/oauth2client/contrib/locked_file.py
[PythonDir]/Lib/site-packages/oauth2client/contrib/_win32_opener.py
tl;dr
只是remove/rename文件
[PythonDir]/Lib/site-packages/oauth2client/contrib/_win32_opener.py