【问题标题】:Python Google Drive API discovery.build fails with exit code -1073740777 (0xC0000417)Python Google Drive API discovery.build 失败,退出代码为 -1073740777 (0xC0000417)
【发布时间】:2016-02-16 22:09:42
【问题描述】:

我正在构建一个将图像上传到谷歌驱动器的 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()

【问题讨论】:

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


    【解决方案1】:

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

    我不知道在主函数中添加这一行有什么帮助

    sys.modules['win32file'] = None
    

    【讨论】:

      【解决方案2】:

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

      经过一点调试,发现如下:

      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;dr

      只需删除/重命名文件

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-03
        • 1970-01-01
        • 2017-07-14
        • 2010-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多