Python Google Drive API discovery.build失败,退出代码为-1073740777(0xC0000417)

时间:2015-11-15 20:35:40

标签: python oauth-2.0 google-drive-api

我正在构建一个将图像上传到谷歌驱动器的python应用程序。然而,在工作了一段时间后,我的谷歌驱动器上传突然停止工作。每当我尝试初始化服务时,程序都会以代码-1073740777(0xC0000417)退出。

我已经尝试使用开发者控制台(也使用完全不同的Google帐户)创建新的client_secret.json文件,并删除drive-python-quickstart.json凭据文件。

我的朋友没有使用相同代码的问题,正如我所说,这对我来说已经有一段时间了,但突然停止了工作。

我使用Python 3.5 32位运行Windows 10 Pro x64。

运行此示例程序时出现问题(取自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()

2 个答案:

答案 0 :(得分:2)

使用Google Youtube API时遇到同样的问题,也有相同的操作系统(Win 10 Pro x64)并使用相同的版本哦python(3.5)。

我不知道如何在主函数中添加此行有帮助

sys.modules['win32file'] = None

答案 1 :(得分:0)

好的,我最终自己解决了这个问题。

经过一些调试后,我发现了以下内容:

oauth2client可以使用两种不同的方法来打开文件。它首先尝试导入类_Win32Opener,当导入失败时,它使用_FcntlOpener。 _Win32Opener的导入不会失败,但使用_Win32Opener打开和锁定文件会失败,因此程序崩溃。要强制oauth2client使用_​​FcntlOpener,只需删除/重命名pyhton包中的文件_win32_opener.py

相关文件:

[PythonDir]/Lib/site-packages/oauth2client/contrib/locked_file.py
[PythonDir]/Lib/site-packages/oauth2client/contrib/_win32_opener.py

<强> TL;博士

只需删除/重命名文件

即可
[PythonDir]/Lib/site-packages/oauth2client/contrib/_win32_opener.py