Anabta Anabta - 4 months ago 25
Python Question

Python Google Drive API discovery.build fails with exit code -1073740777 (0xC0000417)

I am building a python application which uploads images to google drive. However after working for some some time my google drive upload suddenly stopped working. Whenever I try to initialize the service, the program exits with code -1073740777 (0xC0000417).

I have already tried to create a new client_secret.json file with the developer console (also with a completely different Google account) and deleting the drive-python-quickstart.json credential file.

My friends do not have this problem with the same code and as I said, this has worked for me for some time, too, but suddenly stopped working.

I am running Windows 10 Pro x64 with Python 3.5 32 Bit.

The problem occurrs when running this example program (taken from 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()

Answer

Okay, I have fixed the problem myself eventually.

After a little bit of debugging, I found out the following:

The oauth2client can use two different methods for opening a file. It first tries to import the class _Win32Opener and when the import fails it uses the _FcntlOpener. The import of _Win32Opener does not fail but the opening and locking of a file using the _Win32Opener fails so the program crashes. To force oauth2client to use the _FcntlOpener, just remove/rename the file _win32_opener.py in your pyhton packages.

Relevant files:

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

tl;dr

Just remove/rename the file

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